`

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

阅读更多

项目(包)列表:



 

 

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

}

  运行结果:

 

  • 大小: 23 KB
  • 大小: 10.7 KB
  • 大小: 17.2 KB
分享到:
评论

相关推荐

    spring ioc.aop

    spring 的aop的详解如:切面,连接点,通知,切入点,目标对象,代理对象及annotation方式的aop实现和xml方式的事务管理等

    spring重点知识_ioc_springjava_aop_

    spring框架的主要知识点,含ioc,aop,annotation等关键值时点

    Spring IOC AOP

    三种注入类型。IOC/AOP方面的知识。如基础bean的配置,Annotation。挺详细的.

    tiny-spring-practice:实践tiny-spring,手写Spring两大核心功能:IOC和AOP

    然后发现了多年前的一个精简版的Spring学习项目,叫,作者对spring核心的IOC和AOP进行了临摹实现,也很细心的对实现步骤进行了拆分。我看完了tiny-spring收获许多,自己也参考该项目进行了模仿与实践,从我学习的...

    spring练习项目.7z

    资料包含spring-iocdi-annotation-document,iocdi-annotation-mvc,iocdi-xml-extend,iocdi-annotation-extend proxy,jdkproxy-transaction,jdkproxy-salary,day02-itheima11-spring-08-cglibproxy,day02-itheima11-...

    Spring的学习笔记

    (一) Annotation注解方式配置事务管理 31 (二) Spring事务选项 35 (三) XML文件形式配置Spring事务管理 37 四、 HibernateTemplate 38 (一) HibernateTemplate 38 (二) HibernateDaoSupport 39 第十一课:Spring整合...

    xmljava系统源码-springdemo:spring3的IOC,AOP,JDBC,orm等各个模块的使用示例集锦,可以作为入门的spri

    spring3的IOC,AOP,JDBC,orm等各个模块的使用示例集锦,可以作为入门的spring学习示例教程 在org.springweb.context.test包下 1.IOC * spring框架设计理念(POJO化) * 1.IOC 概念,作用,优点 Donnot call me,we ...

    Spring学习笔记

    spring的配置 IOC 依赖注入 基于Xml的注入 基于注释的注入 Spring的自动注入和属性自动注入 AOP 静态代理 动态代理 使用spring实现AOP 基于Annotation实现AOP 基于XML实现AOP SSH的整合 spring集成...

    spring2.5 学习笔记

    (一) Annotation注解方式配置事务管理 31 (二) Spring事务选项 35 (三) XML文件形式配置Spring事务管理 37 四、 HibernateTemplate 38 (一) HibernateTemplate 38 (二) HibernateDaoSupport 39 第十一课:Spring整合...

    doodle:A Simple Java MVC Framework。提供Bean容器、Ioc、Aop、MVC功能

    doodle是一个简易的Java MVC框架,它提供了类似于spring 的Bean容器、IOC、AOP、MVC等功能 代码简洁、轻量,适合用于参考学习spring 一行代码即可启动服务,内置Tomcat容器 DispatcherServlet请求逻辑处理采用责任链...

    Spring Framewor开发手册

    2. Spring 2.0和 2.5的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 新的bean作用域 2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件 2.3. ...

    spring2.5.chm帮助文档(中文版)

    2. Spring 2.0和 2.5的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 新的bean作用域 2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件...

    Spring.3.x企业应用开发实战(完整版).part2

    第2篇 IoC和AOP 第3章 IoC容器概述 3.1 IoC概述 3.1.1 通过实例理解IoC的概念 3.1.2 IoC的类型 3.1.3 通过容器完成依赖关系的注入 3.2 相关Java基础知识 3.2.1 简单实例 3.2.2 类装载器ClassLoader 3.2.3 Java反射...

    spring_note.rar_inversion_spring concept

    Spring应用IOC/DI(重要) xml annotation Spring应用AOP(重要) xml annotation Struts2.1.6 + Spring2.5.6 + Hibernate3.3.2整合(重要) opensessionInviewfilter(记住,解决什么问题,怎么解决) Spring JDBC

    spring+hibernate+JPA+BoneCP

    原创资源,码超所值,价廉物美。...所用的技术比较多,如Spring的IOC,AOP,Transactiion,Annotation,Spring_JUnit_Test及Log4j;Hibernate的JPA Annotation;BoneCP的数据库连接测等。是很好的学习资料!

    Spring中文帮助文档

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 ...

    Spring3.x企业应用开发实战(完整版) part1

    第2篇 IoC和AOP 第3章 IoC容器概述 3.1 IoC概述 3.1.1 通过实例理解IoC的概念 3.1.2 IoC的类型 3.1.3 通过容器完成依赖关系的注入 3.2 相关Java基础知识 3.2.1 简单实例 3.2.2 类装载器ClassLoader 3.2.3 Java反射...

    Spring API

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 ...

    流程管理系统,rbac,dwr

    利用Spring的AOP来做声明式事务,利用Spring的Annotation方式基本达到了零配置,持久层全部使用Hibernate Annotation方式,主要采用的是标准的JPA注解,这样减少了大量的Hibernate映射文件,提高了可维护性,Java与...

    SSM三大框架整合所需jar包+日志/缓存/验证码/ajax/数据校验全部jar包

    spring-aop-4.0.0.RELEASE.jar spring-beans-4.0.0.RELEASE.jar spring-context-4.0.0.RELEASE.jar spring-core-4.0.0.RELEASE.jar spring-expression-4.0.0.RELEASE.jar 【jdbc核心】 spring-jdbc-4.0.0....

Global site tag (gtag.js) - Google Analytics