采用annotation的方式配置Spring的IOC和AOP/采用XML的方式配置Spring的IOC和AOP
2016-08-02 16:17
411 查看
http://wosyingjun.iteye.com/blog/1837711
项目(包)列表:
Xml代码
<?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代码
<?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>
Java代码
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!!!!");
}
}*/
Java代码
package yingjun.service;
import yingjun.model.User;
public interface UserServiceI {
/*用户操作*/
public void DoUser(User use);
}
Java代码
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;
}
}
Java代码
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 );
}
Java代码
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("保存用户成功");
}
}
Java代码
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;
}
}
Java代码
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代码
<?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代码
<?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>
Java代码
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!!!!");
}
}
Java代码
package yingjun.service;
import yingjun.model.User;
public interface UserServiceI {
/*用户操作*/
public void DoUser(User use);
}
Java代码
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;
}
}
Java代码
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 );
}
Java代码
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("保存用户成功");
}
}
Java代码
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;
}
}
Java代码
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代码
<?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代码
<?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>
Java代码
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!!!!");
}
}*/
Java代码
package yingjun.service;
import yingjun.model.User;
public interface UserServiceI {
/*用户操作*/
public void DoUser(User use);
}
Java代码
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;
}
}
Java代码
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 );
}
Java代码
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("保存用户成功");
}
}
Java代码
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;
}
}
Java代码
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代码
<?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代码
<?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>
Java代码
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!!!!");
}
}
Java代码
package yingjun.service;
import yingjun.model.User;
public interface UserServiceI {
/*用户操作*/
public void DoUser(User use);
}
Java代码
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;
}
}
Java代码
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 );
}
Java代码
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("保存用户成功");
}
}
Java代码
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;
}
}
Java代码
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);
}
}
运行结果:
相关文章推荐
- spring aop xml方式的配置案例 以及 annotation方式的配置aop
- Spring Aop实现——Annotation方式(注解式)and Schema-based式(xml配置)
- 配置Spring AOP,XML方式比Annotation方式略好
- Spring Aop实现——Annotation方式(注解式)and Schema-based式(xml配置)
- Spring之AOP声明式事务管理(采用xml的配置方式更好)
- Spring学习一:IOC(控制反转)和AOP(面向切面)的xml配置和注解方式
- Spring 采用基于XML方式配置事务
- spring采用基于xml配置方式进行事务的处理
- 【学习】Spring 的 AOP :基于Annotation 的“零配置”方式
- Spring3与Hibernate4整合,Spring中配置Hibernate基于XML和annotation的sessionFactory方式。
- spring AOP实现(Annotation方式/静态文件配置方式)
- 关于SpringAOP的XML方式的配置
- Spring---->采用Annotation的方式实现AOP
- 8.4.6: Spring的AOP---基于XML配置文件的管理方式
- Spring---->采用静态配置文件方式实现AOP
- spring之aop编程——基于注解、xml配置文件方式
- spring aop的使用(注解方式以及基于xml配置方式)
- spring学习笔记12----Spring+jdbc组合开发( 采用基于xml方式配置事务)
- 8.4.5: Spring的AOP---基于Annotation的零配置方式
- Spring---->采用静态配置文件方式实现AOP