论坛首页 Java企业应用论坛

采用XML的方式配置Spring的IOC和AOP

浏览 4211 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (7) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-03-27  

项目(包)列表:

 

    

<?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.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);
	}

}

 运行结果:

 

 

  • 大小: 23 KB
  • 大小: 10.7 KB
  • 大小: 16.7 KB
   发表时间:2013-04-03  
嗯  spring2.x时代的经典配置。 到了3.0之后都是注解为主了。 不过个人感觉注解会导致代码变乱,所以说还是倾向于原始配置啊。
0 请登录后投票
   发表时间:2013-04-05  
关于spring 的AOP 在实际项目中 几乎所有采用的是 XML 的配置。个人的经历Q!
0 请登录后投票
   发表时间:2013-04-05  
superdandy 写道
嗯  spring2.x时代的经典配置。 到了3.0之后都是注解为主了。 不过个人感觉注解会导致代码变乱,所以说还是倾向于原始配置啊。

是的 AOP方面我比较倾向于用XML
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics