IDEA中如何引入spring的命名空間
IDEA引入spring的命名空間
我們?cè)趯憇pring的配置文件的時(shí)候,有的時(shí)候可能會(huì)用到 P 標(biāo)簽,然后我們發(fā)現(xiàn)自己并沒有p標(biāo)簽啊,那么我們一起來(lái)看我是怎么解決的。
首先在我們的xml文件的首部添上這句話:
xmlns:context="http://www.springframework.org/schema/context"
然后我們打出
xmlns:p=
然后就會(huì)相應(yīng)的提示:
還有一點(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&characterEncoding=utf-8&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,但是類成員屬性的注入方法是不推薦的
在stackoverflow上有人做了詳細(xì)的解釋 https://stackoverflow.com/questions/39890849/what-exactly-is-field-injection-and-how-to-avoid-it
總結(jié)下來(lái),使用屬性注入會(huì)產(chǎn)生如下問題
- 對(duì)象和注入的容器有著很緊的耦合
- 對(duì)象間的耦合被隱藏了,外部無(wú)法看到,不利于復(fù)雜度控制
- 如果沒有注入容器,對(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(); } }); }
問題與解決
nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
這個(gè)錯(cuò)誤顯然是沒有找到某個(gè)jar包。如果要定義aop,除了spring核心包之外,還需要自行下載這兩個(gè)jar。
- aopalliance.jar
- aspectjweaver.jar
檢查一下jar包,發(fā)現(xiàn)沒有aspectjweaver.jar,下載并加入到項(xiàng)目路徑即可。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
WIN7系統(tǒng)JavaEE(java)環(huán)境配置教程(一)
這篇文章主要介紹了WIN7系統(tǒng)JavaEE(java+tomcat7+Eclipse)環(huán)境配置教程,本文重點(diǎn)在于java配置,感興趣的小伙伴們可以參考一下2016-06-06SpringBoot 使用WebSocket功能(實(shí)現(xiàn)步驟)
本文通過(guò)詳細(xì)步驟介紹了SpringBoot 使用WebSocket功能,首先需要導(dǎo)入WebSocket坐標(biāo),編寫WebSocket配置類,用于注冊(cè)WebSocket的Bean,結(jié)合示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-02-02java對(duì)象轉(zhuǎn)化成String類型的四種方法小結(jié)
在java項(xiàng)目的實(shí)際開發(fā)和應(yīng)用中,常常需要用到將對(duì)象轉(zhuǎn)為String這一基本功能。本文就詳細(xì)的介紹幾種方法,感興趣的可以了解一下2021-08-08Java Spring @Autowired的這些騷操作,你都知道嗎
這篇文章主要介紹了徹底搞明白Spring中的自動(dòng)裝配和Autowired注解的使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2021-09-09