溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

IDEA中怎么引入spring的命名空間

發(fā)布時(shí)間:2023-04-11 11:32:11 來(lái)源:億速云 閱讀:139 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹“IDEA中怎么引入spring的命名空間”,在日常操作中,相信很多人在IDEA中怎么引入spring的命名空間問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”IDEA中怎么引入spring的命名空間”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

    IDEA引入spring的命名空間

    我們?cè)趯?xiě)spring的配置文件的時(shí)候,有的時(shí)候可能會(huì)用到 P 標(biāo)簽,然后我們發(fā)現(xiàn)自己并沒(méi)有p標(biāo)簽啊,那么我們一起來(lái)看我是怎么解決的。

    首先在我們的xml文件的首部添上這句話:

    xmlns:context="http://www.springframework.org/schema/context"

    然后我們打出

    xmlns:p=

    然后就會(huì)相應(yīng)的提示:

    IDEA中怎么引入spring的命名空間

    還有一點(diǎn)需要注意的就是:

    需要注意的是必須在xmlns:context="”這一行的下面打,否則也不會(huì)提示,如圖所示位置即可提示,否則可能不提示

    最終的代碼:

    xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    idea項(xiàng)目添加spring

    配置步驟

    1.添加spring的依賴包

    idea可以直接右擊項(xiàng)目 選擇add frame support,勾選spring即可

    2.創(chuàng)建applicationContext.xml

    在src的直接子目錄下創(chuàng)建 applicationContext.xml

    這里給出一個(gè)applicationContext.xml 的實(shí)例,以及注釋解釋

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd        
          http://www.springframework.org/schema/aop         
          http://www.springframework.org/schema/aop/spring-aop.xsd        
          http://www.springframework.org/schema/context         
          http://www.springframework.org/schema/context/spring-context.xsd        
          http://www.springframework.org/schema/tx         
          http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    
        <!-- 掃描有注解的文件 base-package 包路徑 -->
        <context:component-scan base-package="service.imp, action, dao.imp"/>
    
    
        <!-- 定義 Autowired 自動(dòng)注入 bean -->
        <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
    
    
        <!-- 聲明式容器事務(wù)管理 ,transaction-manager指定事務(wù)管理器為transactionManager -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="*User"/>
                <tx:method name="*" propagation="NOT_SUPPORTED" read-only="true"/>
            </tx:attributes>
        </tx:advice>
    
    
        <!-- 定義切面,在service包及子包中所有方法中,執(zhí)行有關(guān)的hibernate session的事務(wù)操作 -->
        <aop:config>
            <!-- 只對(duì)業(yè)務(wù)邏輯層實(shí)施事務(wù) -->
            <aop:pointcut id="serviceOperation" expression="execution( * service..*.*(..))"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
        </aop:config>
    
    
        <!-- 配置dataSource -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"/>
            <property name="jdbcUrl"
                      value="jdbc:mysql://localhost:3306/j2ee?useUnicode=true&amp;characterEncoding=utf-8&amp;autoReconnect=true"/>
            <property name="user" value="root"/>
            <property name="password" value="wyy"/>
            <property name="initialPoolSize" value="5"/>
            <property name="maxPoolSize" value="10"/>
        </bean>
    
    
        <!-- 配置sessionFactory -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="packagesToScan" value="model"/>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect"> org.hibernate.dialect.MySQL57Dialect</prop>
                    <prop key="hibernate.show_sql">false</prop>
                    <prop key="hibernate.format_sql">true</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    <prop key="hibernate.connection.autocommit">true</prop>
                </props>
            </property>
        </bean>
    
        <!-- 配置hibernateTemplate -->
        <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
    
    </beans>

    3.給service的實(shí)現(xiàn)類添加@Service注解 給dao的實(shí)現(xiàn)類添加@Repository注解 將生命周期管理交給spring

    注意所有交給spring管理的類,不能new出實(shí)例,只能用spring注入。

    4.所有使用到service和dao的地方,均使用@Autowired注解注入。

    @Autowired注解可以在構(gòu)造函數(shù)、類成員屬性、getset方法添加注解注入bean,但是類成員屬性的注入方法是不推薦的

    IDEA中怎么引入spring的命名空間

    總結(jié)下來(lái),使用屬性注入會(huì)產(chǎn)生如下問(wèn)題

    • 對(duì)象和注入的容器有著很緊的耦合

    • 對(duì)象間的耦合被隱藏了,外部無(wú)法看到,不利于復(fù)雜度控制

    • 如果沒(méi)有注入容器,對(duì)象無(wú)法創(chuàng)建

    • 當(dāng)一個(gè)類有多個(gè)屬性注入,你感知不到他的復(fù)雜度。而當(dāng)你使用構(gòu)造函數(shù)注入時(shí),就會(huì)發(fā)現(xiàn),要穿入的參數(shù)過(guò)多。也是不利于復(fù)雜度控制

    5.dao的實(shí)現(xiàn)技術(shù)

    • sessionFactory

    @Repository
    public class UserDaoImp implements UserDao {
    
        private SessionFactory sessionFactory;
    
        @Autowired
        public UserDaoImp(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }
    
        @Override
        public User get(String userId) {
            return sessionFactory.openSession().load(User.class, userId);
        }
    }
    • hibernateTemplate

    @Repository
    public class UserDaoImp implements UserDao {
    
        @Autowired
        private HibernateTemplate hibernateTemplate;
    
        public UserDaoImp(HibernateTemplate hibernateTemplate) {
            this.hibernateTemplate = hibernateTemplate;
        }
    
        @Override
        public User get(String userId) {
            return hibernateTemplate.get(User.class, userId);
        }
    }

    hibernateTemplate封裝了SessionFactory,數(shù)據(jù)庫(kù)操作變得更簡(jiǎn)單。

    如下給出實(shí)現(xiàn)hibernateTemplate分頁(yè)的代碼。

    @Override
    public List<Order> getListByHql(String hql, int page, int pageSize) {
        return hibernateTemplate.execute(new HibernateCallback<List<Order>>() {
            @Override
            public List<Order> doInHibernate(Session session) throws HibernateException {
                Query<Order> query = session.createQuery(hql);
                query.setFirstResult((page - 1) * pageSize).setMaxResults(pageSize);
                //把結(jié)果返回
                return query.list();
            }
        });
    }

    問(wèn)題與解決

    nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException

    這個(gè)錯(cuò)誤顯然是沒(méi)有找到某個(gè)jar包。如果要定義aop,除了spring核心包之外,還需要自行下載這兩個(gè)jar。

    • aopalliance.jar

    • aspectjweaver.jar

    檢查一下jar包,發(fā)現(xiàn)沒(méi)有aspectjweaver.jar,下載并加入到項(xiàng)目路徑即可。

    到此,關(guān)于“IDEA中怎么引入spring的命名空間”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

    向AI問(wèn)一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI