public class Cat {
public void shot(){
System.out.println("miao~");
}
}
public class Dog {
public void shot(){
System.out.println("wang~");
}
}
@Data
public class People {
private Cat cat;
private Dog dog;
private String name;
}
<bean id="cat" class="com.hdu.pojo.Cat"/>
<bean id="dog" class="com.hdu.pojo.Dog"/>
<bean id="people" class="com.hdu.pojo.People">
<property name="name" value="小王"/>
<property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>
bean>
<bean id="cat" class="com.hdu.pojo.Cat"/>
<bean id="dog" class="com.hdu.pojo.Dog"/>
<bean id="people" class="com.hdu.pojo.People" autowire="byName">
<property name="name" value="小王"/>
bean>
<bean id="cat23" class="com.hdu.pojo.Cat"/>
<bean id="dog111" class="com.hdu.pojo.Dog"/>
<bean id="people" class="com.hdu.pojo.People" autowire="byType">
<property name="name" value="小王"/>
bean>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/beans/spring-context.xsd">
<context:annotation-config/>
beans>
<bean id="cat" class="com.hdu.pojo.Cat"/>
<bean id="dog" class="com.hdu.pojo.Dog"/>
<bean id="people" class="com.hdu.pojo.People"/>
@Data
public class People {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
}
@Data
public class People {
@Autowired
@Qualifier(value = "cat112")
private Cat cat;
@Autowired
private Dog dog;
private String name;
}
<bean id="cat112" class="com.hdu.pojo.Cat"/>
<bean id="dog" class="com.hdu.pojo.Dog"/>
<bean id="people" class="com.hdu.pojo.People"/>
//@Component 等价于
@Component
@Data
public class User {
private String name;
}
@Component
@Data
public class User {
//@Value 等价于
@Value("小王")
private String name;
}
@Component
@Data
//@Scope 定义类的作用域 singleton 单例模式 prototype 原型模式
@Scope("singleton")
public class User {
@Value("小王")
private String name;
}
<context:component-scan base-package="com.hdu"/>
<context:annotation-config/>
@Configuration//配置类
public class MyConfig {
//注册一个bean,等价于xml文件中的配置方式
//方法的名字,相当于bean标签中的id
//方法的返回值,相当于bean标签中的class属性
@Bean
public User getUser(){
return new User();//要注入到bean的对象
}
}
public class MyTest {
public static void main(String[] args) {
//如果使用配置类实现,AnnotationConfig用来获得容器,通过配置类对象加载
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
User getUser = (User) context.getBean("getUser");
System.out.println(getUser.getName());
}
}
介绍参考了AOP【面向切面编程】
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.4version>
dependency>
AOP的实现实例:
作者:架构师小跟班
出处:http://www.cnblogs.com/xyhero/
个人网站: http://www.jiagou1216.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果文中有什么错误,欢迎指出,以免更多的人被误导。
public interface UserService {
public void addUser();
public Object findUser();
}
public class UserServiceImpl1 implements UserService{
@Value("小王")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void addUser() {
System.out.println("----->执行业务方法addUser");
}
@Override
public String findUser() {
System.out.println("----->执行业务方法findUser,查找到的用户是:"+name);
return name;
}
}
public class Log {
/*
前置通知:目标方法调用之前执行的代码
*/
public void doBefore(JoinPoint jp){
System.out.println("======前置通知========");
}
/*
后置返回通知:目标方法正常结束后执行的代码
返回通知可以访问到目标方法的返回值
*/
public void doAfterReturning(JoinPoint jp,String result){
System.out.println("======执行后置通知=======");
System.out.println("======返回值result:"+result);
}
/*
最终通知:目标方法调用之后执行的代码(无论是否出现异常)
因为方法可能会出现异常,所以不能返回方法的返回值
*/
public void doAfter(JoinPoint jp){
System.out.println("======执行最终通知======");
}
/*
异常通知:目标方法抛出异常时执行的代码
可以访问到异常对象
*/
public void doAfterThrow(JoinPoint jp,Exception ex){
System.out.println("======执行异常通知======");
System.out.println("======异常:"+ex.toString());
}
/*
环绕通知:目标方法调用前后执行的代码,可以在方法调用前后完成自定义的行为
保卫一个切入点的通知,会在切入点方法执行前后均执行代码
主要是调用proceed()方法来执行切入点方法
环绕通知类似于动态代理的全过程:ProceedingJoinPoint类型的参数可以决定是否执行目标方法
而且环绕通知必须有返回值,返回值即为目标方法的返回值
*/
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("======执行环绕通知开始======");
//调用方法的参数
Object[] args=pjp.getArgs();
//调用方法名
String method = pjp.getSignature().getName();
//获取目标对象
Object target = pjp.getTarget();
//执行完方法的返回值
//调用proceed()方法,就会触发切入点方法执行
Object result = pjp.proceed();
System.out.println("======方法名:"+method+";目标对象:"+target+";返回值:"+result);
System.out.println("======执行环绕通知结束");
return result;
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.hdu"/>
<context:annotation-config/>
<bean id="userService" class="com.hdu.service.UserServiceImpl1"/>
<bean id="log" class="com.hdu.log.Log"/>
<aop:config>
<aop:aspect ref="log">
<aop:pointcut id="pointcut" expression="execution(* com.hdu.service.UserServiceImpl1..*(..))"/>
<aop:before method="doBefore" pointcut-ref="pointcut"/>
<aop:after-returning method="doAfterReturning" pointcut-ref="pointcut" returning="result"/>
<aop:after method="doAfter" pointcut-ref="pointcut"/>
<aop:around method="doAround" pointcut-ref="pointcut"/>
<aop:after-throwing method="doAfterThrow" pointcut-ref="pointcut" throwing="ex"/>
aop:aspect>
aop:config>
beans>
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("aopConfig.xml");
UserService userService = (UserService) context.getBean("userService");
userService.addUser();
System.out.println("\n");
userService.findUser();
}
}
<aop:pointcut id="pointcut" expression="execution(* com.hdu.service.UserServiceImpl1..*(..))"/>
//标志这个类是一个切面
@Aspect
public class AnnotationLog {
/*
必须为final String类型,注解里要使用的变量只能是静态常量型
*/
public static final String EDP="execution(* com.hdu.service.UserServiceImpl2..*(..))";
//前置通知
@Before(EDP)
public void doBefore(JoinPoint jp){
System.out.println("======前置通知========");
}
//返回通知
@AfterReturning(value = EDP,returning = "result")
public void doAfterReturning(JoinPoint jp,String result){
System.out.println("======执行后置通知=======");
System.out.println("======返回值result:"+result);
}
//最终通知
@After(EDP)
public void doAfter(JoinPoint jp){
System.out.println("======执行最终通知======");
}
//异常通知
@AfterThrowing(value = EDP,throwing = "ex")
public void doAfterThrow(JoinPoint jp,Exception ex){
System.out.println("======执行异常通知======");
System.out.println("======异常:"+ex.toString());
}
//环绕通知
@Around(EDP)
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("======执行环绕通知开始======");
//调用方法的参数
Object[] args=pjp.getArgs();
//调用方法名
String method = pjp.getSignature().getName();
//获取目标对象
Object target = pjp.getTarget();
//执行完方法的返回值
//调用proceed()方法,就会触发切入点方法执行
Object result = pjp.proceed();
System.out.println("======方法名:"+method+";目标对象:"+target+";返回值:"+result);
System.out.println("======执行环绕通知结束");
return result;
}
}
<context:component-scan base-package="com.hdu"/>
<context:annotation-config/>
<aop:aspectj-autoproxy/>
<bean id="userService" class="com.hdu.service.UserServiceImpl2"/>
<bean id="log" class="com.hdu.log.AnnotationLog"/>