Bean的初始化和依赖注入
一、依赖注入
pom文件
需要引入spring-context
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
1.编写功能的Bean
package com.eleven.bean;
import org.springframework.stereotype.Service;
@Service // 表示CoolService类是Spring管理的一个Bean
public class CoolService {
public String sayHello(String word) {
return "Hello" + word + " !";
}
}
2.使用功能类的Bean
package com.eleven.bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service // 表示UseCoolService是Spring管理的一个Bean
public class UseCoolService {
@Autowired // 将CoolService注入到UseCoolService里面
CoolService coolService;
public String sayHello(String word) {
return coolService.sayHello(word);
}
}
3.配置类
package com.eleven.bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration // 表示当前类是一个配置类
@ComponentScan("com.eleven.bean") // 自动扫描包下面所有的类,注册为Bean
public class DiConfigs {
}
4.运行
package com.eleven.bean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Mains {
public static void main(String[] args) {
// 使用AnnotationConfigApplicationContext作为Spring的容器,将DiConfigs配置类输入。
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DiConfigs.class);
// 获得声明配置的UseCoolService的Bean
UseCoolService useCoolService = context.getBean(UseCoolService.class);
// 打印输出
System.out.println(useCoolService.sayHello("Di"));
// 关闭连接
context.close();
}
}
二、Java配置
1.编写功能类的Bean
package com.eleven.javabeans;
public class KeepService {
public String sayHello(String word) {
return "hello" + word + "!";
}
}
2.使用功能类的Bean
package com.eleven.javabeans;
public class UseKeepService {
KeepService keepService;
public void setKeepService(KeepService keepService) {
this.keepService = keepService;
}
public String sayHello(String word) {
return keepService.sayHello(word);
}
}
3.配置类
package com.eleven.javabeans;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration // 声明是一个配置类,意味着可能有0个或多个Bean注解
public class JavaConfigs {
@Bean // 使用Bean注解声明当前方法KeepService的返回值是一个Bean,Bean的名称就是方法名
public KeepService keepService() {
return new KeepService();
}
@Bean
public UseKeepService useKeepService(KeepService keepService) {
UseKeepService useKeepService = new UseKeepService();
useKeepService.setKeepService(keepService); // 注入KeepService的Bean直接调用keepService
return useKeepService;
}
}
4.运行
package com.eleven.javabeans;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
// 使用AnnotationConfigApplicationContext作为spring的容器,将JavaConfigs配置类输入
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfigs.class);
// 获得声明配置的UseKeepService
UseKeepService useKeepService = context.getBean(UseKeepService.class);
System.out.println(useKeepService.sayHello(" Java Config"));
context.close();
}
}
三、AOP
1.引入pom文件(spring-aof和aspectj)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId> org.aspectj</groupId>
<artifactId> aspectjweaver</artifactId>
<version> 1.9.0</version>
</dependency>
2.编写拦截规则的注解
package com.eleven.aop1;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD) // 用来描述注解的使用范围,用来描述的注解可以用在什么地方
@Retention(RetentionPolicy.RUNTIME) // 修饰注解,注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在
@Documented // 表示这个注解式由javadoc记录的
public @interface Action { // 定义一个注解@Action
String name();
}
3.编写使用注解的拦截类
package com.eleven.aop1;
import org.springframework.stereotype.Service;
@Service // 表示DemoAnnotationService是Spring管理的一个Bean
public class DemoAnnotationService {
@Action(name = "注解式拦截的add操作")
public void add() {}
}
4.编写使用方法规则被拦截类
package com.eleven.aop1;
import org.springframework.stereotype.Service;
@Service // 表示DemoMethodService是Spring管理的一个Bean
public class DemoMethodService {
public void add() {}
}
5.编写切面
package com.eleven.aop1;
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
@Aspect // 声明一个切面
@Component // 让这个切面成为Spring容器管理的Bean
public class LogAspect {
@Pointcut("@annotation(com.eleven.aop1.Action)") // 注解声明切点
public void annotationPointCut() {
}
@After("annotationPointCut()")
public void after(JoinPoint joinPoint) {
MethodSignature sinSignature = (MethodSignature) joinPoint.getSignature();
Method method = sinSignature.getMethod();
Action acion = method.getAnnotation(Action.class); // 通过反射可以获得注解上的属性,然后做日志记录操作
System.out.println("注解式拦截 " + acion.name());
}
@Before("execution(* com.eleven.aop1.DemoMethodService.*(..))") // 声明一个建言,此建言直接使用拦截规则做参数
public void before(JoinPoint joinPoint) {
MethodSignature sinSignature = (MethodSignature) joinPoint.getSignature();
Method method = sinSignature.getMethod();
System.out.println("方法规则拦截 " + method.getName());
}
}
6.配置类
package com.eleven.aop1;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration // 声明当前是一个配置类
@ComponentScan("com.eleven.aop1") // 自动扫描包下面所有的类
@EnableAspectJAutoProxy // 开启Spring对AspectJ代理的支持
public class AopConfig {
}
7.运行
package com.eleven.aop1;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
// 使用AnnotationConfigApplicationContext作为Spring的容器,将AopConfig配置类输入
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);
// 获得声明配置的DemoAnnotationService
DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);
// 获得声明配置的DemoMethodService
DemoMethodService demoMethodService = context.getBean(DemoMethodService.class);
demoAnnotationService.add();
demoMethodService.add();
context.close();
}
}