http://wosyingjun.iteye.com/blog/1837711
项目(包)列表:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <!--spring 配置文件位置-->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring.xml</param-value>
- </context-param>
- <!--spring 监听器-->
- <listener>
- <description>spring监听器</description>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
- <?xml version="1.0" encoding="utf-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- ">
- <!-- 采用的annotation的方式进行AOP和 IOC -->
- <!-- 配置了component-scan可以移除 -->
- <context:annotation-config />
- <!-- 启动对@AspectJ注解的支持 -->
- <aop:aspectj-autoproxy />
- <!-- 自动扫描包(自动注入) -->
- <context:component-scan base-package="yingjun" />
- </beans>
- package yingjun.aop;
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.AfterReturning;
- import org.aspectj.lang.annotation.AfterThrowing;
- import org.aspectj.lang.annotation.Around;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- import org.aspectj.lang.annotation.Pointcut;
- import org.springframework.stereotype.Component;
- @Component("aop")
- @Aspect
- public class AopMethod {
- @Pointcut("execution( * yingjun.service..*.*(..))")
- public void myMethod(){}
- @Before("execution( * yingjun.service..*.*(..))")//表示yingjun.service下任何包下任何类任何返回值的任何方法
- public void beforeMethod(){
- System.out.println("before method...AOP!!!!");
- }
- @AfterReturning("myMethod()")
- public void AfterReturningMethod(){
- System.out.println("After Returning normal...AOP!!!!");
- }
- @AfterThrowing("myMethod()")
- public void AfterThrowing(){
- System.out.println("After Throwing...AOP!!!!");
- }
- @Around("myMethod()")
- public void Around(ProceedingJoinPoint pjp) throws Throwable{
- System.out.println("method around start!!!!");
- pjp.proceed();
- System.out.println("method around end!!!!");
- }
- }
- /*public class AopMethod {
- public void beforeMethod(){
- System.out.println("before method...AOP!!!!");
- }
- public void AfterReturningMethod(){
- System.out.println("After Returning normal...AOP!!!!");
- }
- public void AfterMethod(){
- System.out.println("After method...AOP!!!!");
- }
- public void AfterThrowing(){
- System.out.println("After Throwing...AOP!!!!");
- }
- }*/
- package yingjun.service;
- import yingjun.model.User;
- public interface UserServiceI {
- /*用户操作*/
- public void DoUser(User use);
- }
- package yingjun.service.impl;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import yingjun.dao.UserDaoI;
- import yingjun.model.User;
- import yingjun.service.UserServiceI;
- @Component("userService")
- public class UserServiceImpl implements UserServiceI {
- private UserDaoI userdao;
- public void DoUser(User user) {
- userdao.SaveUser(user);
- }
- public UserDaoI getUserdao() {
- return userdao;
- }
- //@Autowired按byType自动注入,如果想用byName则使用@Qulifie,
- //而@Resource默认按name,name找不到,按类型
- @Autowired
- public void setUserdao(UserDaoI userdao) {
- this.userdao = userdao;
- }
- }
- package yingjun.dao;
- import yingjun.model.User;
- public interface UserDaoI {
- public void AddUser(User user );
- public void DeleteUser(User user );
- public void SaveUser(User user );
- }
- package yingjun.dao.impl;
- import org.springframework.stereotype.Component;
- import org.springframework.stereotype.Repository;
- import yingjun.dao.UserDaoI;
- import yingjun.model.User;
- @Component("userdao")
- public class UserDaoImpl implements UserDaoI{
- public void AddUser(User user ) {
- System.out.println("增加用户成功");
- }
- public void DeleteUser(User user ) {
- System.out.println("删除用户成功");
- }
- public void SaveUser(User user ) {
- System.out.println("保存用户成功");
- }
- }
- package yingjun.model;
- public class User {
- private int id;
- private String name;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- package yingjun.test;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import yingjun.model.User;
- import yingjun.service.UserServiceI;
- public class SpringTest {
- @Test
- public void springtest(){
- ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
- UserServiceI us=(UserServiceI)ac.getBean("userService");
- User user=new User();
- us.DoUser(user);
- }
- }
=================================================================================================================
采用XML的方式配置Spring的IOC和AOP
http://wosyingjun.iteye.com/blog/1837682
项目(包)列表:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <!--spring 配置文件位置-->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring.xml</param-value>
- </context-param>
- <!--spring 监听器-->
- <listener>
- <description>spring监听器</description>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
- <?xml version="1.0" encoding="utf-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- ">
- <bean id="AopMethod" class="yingjun.aop.AopMethod"> </bean>
- <bean id="u" class="yingjun.dao.impl.UserDaoImpl"> </bean>
- <bean id="userService" class="yingjun.service.impl.UserServiceImpl">
- <property name="userdao" ref="u"></property>
- </bean>
- <aop:config>
- <aop:pointcut expression="execution( * yingjun.service..*.*(..))" id="servicepointcut"/>
- <aop:aspect id="myaspect" ref="AopMethod">
- <!--对应expression执行前运行 -->
- <aop:before method="beforeMethod" pointcut-ref="servicepointcut"/>
- <!--对应expression执行后返回正常运行 -->
- <aop:after-returning method="AfterReturningMethod" pointcut-ref="servicepointcut"/>
- <!--对应expression抛出异常运行 -->
- <aop:after-throwing method="AfterThrowing" pointcut-ref="servicepointcut"/>
- <!--对应expression执行后运行(不管是否抛出异常都会执行) -->
- <aop:after method="AfterMethod" pointcut-ref="servicepointcut"/>
- </aop:aspect>
- </aop:config>
- </beans>
- package yingjun.aop;
- public class AopMethod {
- public void beforeMethod(){
- System.out.println("before method...AOP!!!!");
- }
- public void AfterReturningMethod(){
- System.out.println("After Returning normal...AOP!!!!");
- }
- public void AfterMethod(){
- System.out.println("After method...AOP!!!!");
- }
- public void AfterThrowing(){
- System.out.println("After Throwing...AOP!!!!");
- }
- }
- package yingjun.service;
- import yingjun.model.User;
- public interface UserServiceI {
- /*用户操作*/
- public void DoUser(User use);
- }
- package yingjun.service.impl;
- import yingjun.dao.UserDaoI;
- import yingjun.model.User;
- import yingjun.service.UserServiceI;
- public class UserServiceImpl implements UserServiceI {
- private UserDaoI userdao;
- public void DoUser(User user) {
- userdao.SaveUser(user);
- }
- public UserDaoI getUserdao() {
- return userdao;
- }
- public void setUserdao(UserDaoI userdao) {
- this.userdao = userdao;
- }
- }
- package yingjun.dao;
- import yingjun.model.User;
- public interface UserDaoI {
- public void AddUser(User user );
- public void DeleteUser(User user );
- public void SaveUser(User user );
- }
- package yingjun.dao.impl;
- import yingjun.dao.UserDaoI;
- import yingjun.model.User;
- public class UserDaoImpl implements UserDaoI{
- public void AddUser(User user ) {
- System.out.println("增加用户成功");
- }
- public void DeleteUser(User user ) {
- System.out.println("删除用户成功");
- }
- public void SaveUser(User user ) {
- System.out.println("保存用户成功");
- }
- }
- package yingjun.model;
- public class User {
- private int id;
- private String name;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- package yingjun.test;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import yingjun.model.User;
- import yingjun.service.UserServiceI;
- public class SpringTest {
- @Test
- public void springtest(){
- ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
- UserServiceI us=(UserServiceI)ac.getBean("userService");
- User user=new User();
- us.DoUser(user);
- }
- }