2018-03-03
我用的是eclipse搭建的web項(xiàng)目
(1)導(dǎo)入必要的jar包,總共四個(gè),還要加上2個(gè)常用的日志jar包

(2)在src(建議在src目錄下建立applicationcontext.xml文件(名字可隨便起))


配置(約束).dtd文件Window-->Preferencs-->搜索XML-->Catalog-->add-->



(二,注解形式)
jar包:

②:配置文件添加注解約束“


:③:創(chuàng)建類-->方法-->測試類-->Service類
(注解有@Component/Controller/Service/Repository)
@Component("user")-----注解自動(dòng)創(chuàng)建對象
在service層得到dao對象可以用@autowired,更建議用@Resource(name=" ")在service引入dao對象
<三>Spring 的aop操作:
在spring里面進(jìn)行aop,使用aspectj(aspectj-不是spring的一部分,和spring一起使用進(jìn)行aop操作)
①jar包:aopalliance.jar ?+ ?aspectjweaver.jar ?+ ?spring-aop-3.2.17.RELEASE.jar ?+ ?spring-aspects-3.2.17.RELEASE.jar
②導(dǎo)入aop約束:E.2.7?Theaopschema
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
③,寫配置文件:

*關(guān)于增強(qiáng)的測試代碼:
①public class Book {
? public void add(){
? System.out.println("被增強(qiáng)發(fā)法。。。。。");
? }
}
②public class MyBook {
? public void before(){
? System.out.println("前置增強(qiáng)。。。");
? }
? public void after(){
? System.out.println("后置增強(qiáng)。。。");
? }
? public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
? System.out.println("環(huán)繞方法前。。。");
? //執(zhí)行被增強(qiáng)方法
? proceedingJoinPoint.proceed();
? System.out.println("環(huán)繞方法后。。。");
? }
}
③public class ZengqiangTest {
@Test
public void testZengqiang(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Book book=(Book) context.getBean("book");
book.add();
}
}
④

*(控制臺出現(xiàn)警告是因?yàn)樯倭薼og4j.properties 可自行設(shè)置日志等級)
Spring整合web項(xiàng)目實(shí)際操作:
①添加Struts2的jar包,web.xml配置過濾器,監(jiān)聽(監(jiān)聽是為了在服務(wù)器啟動(dòng)的時(shí)候就創(chuàng)建對象,加載applicationContext.xml配置,提高運(yùn)行效率----配置監(jiān)聽器之前要確定已經(jīng)導(dǎo)入了,spring整合web項(xiàng)目的jar包spring-web-3.2.17.RELEASE.jar)
1.1監(jiān)聽器:



②請記?。罕O(jiān)聽器默認(rèn)是找/WEB-INF/目錄下的applicationContext.xml文件,但是我們寫在了Src目錄下,
所以我們還要指定加載spring配置文件路徑:
<!--指定加載spring配置文件位置(想知道spring加載配置源代碼自己搜吧)-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?----文章作者原創(chuàng),歡迎大家借鑒,指正