Spring4入门 - Hellsinner/Spring4-Example GitHub Wiki
Spring4 入门
Java配置
Spring Ioc
- @Configuration 表明这个类是一个配置类,它可以为Spring提供Bean的定义信息,相当于一个xml文件
- @Bean 声明一个Bean,相当于xml文件中的bean标签
- @ComponentScan 包扫描注解,相当于context component-scan标签,可以扫描指定的包,并把其中声明的Bean注册到Spring容器中
- @ImportResource 在配置类中,可以使用@ImportResource注解将xml配置引入到Spring容器
- 示例
@Configuration
//@ComponentScan("ioc")
@ImportResource("classpath:application.xml")
public class IocConfig {
@Bean
public IocService iocService(IocDao iocDao) {
IocService iocService = new IocService();
iocService.setIocDao(iocDao);
return iocService;
}
}
- 启动容器
public class IocMain {
public static void main(String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext(IocConfig.class);
IocService iocService = context.getBean(IocService.class);
iocService.run();
}
}
使用AnnotationConfigApplicationContext加载一个配置类启动Spring容器,相当于使用ClassPathXmlApplicationContext加载一个配置文件启动Spring容器
Spring Aop
- @Aspect 使用@Aspect将类声明为一个切面,使用@Componment注解将其注册到Spring
- @Pointcut @Pointcut声明一个切点,也就是一个拦截到规则
- @Before、@After、@Around 断言注解,代码方法执行前、执行后、环绕执行
- 示例
@Aspect
@Component
public class AopComponment {
@Pointcut("execution(* aop.*.*(..))")
public void excuPointcut() {
}
@Before("excuPointcut()")
public void before(JoinPoint joinPoint) {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature.getMethod();
System.out.println(method.getName());
System.out.println("before point");
}
@After("@annotation(aop.AopAnnoation)")
public void after(JoinPoint joinPoint){
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature.getMethod();
System.out.println(method.getName());
System.out.println("after point");
}
}
- @EnableAspectJAutoProxy @EnableAspectJAutoProxy开启Spring对AspectJ代理的支持
@Configuration
@ComponentScan("aop")
@EnableAspectJAutoProxy
public class AopConfig {
}
加载属性文件
- @PropertySource @PropertySource可以接受一组属性文件,将其值加载到Spring中
@Configuration
@PropertySource(value = "classpath:propertie.properties")
@ComponentScan("properties")
public class ProConfig {
}
- @Value 使用@Value注解可以给属性注入Spring容器的值
@Service
public class ProService {
@Value("${author.name}")
private String name;
public void run() {
System.out.println(name);
}
}
Bean的生命周期
@PostConstruct Bean构造方法执行完之后执行的方法,是bean标签的init-method方法的实现 @PreDestroy Bean销毁前调用的方法,是bean标签的destory-method的实现
- 示例
@Service
public class BeanService {
@PostConstruct
public void init(){
System.out.println("init BeanService");
}
public void run(){
System.out.println("BeanService");
}
@PreDestroy
public void destroy(){
System.out.println("destroy BeanService");
}
}
Profile
Profile是为在不同环境下使用不同的配置提供的支持
- 通过设定Enviroment的ActiveProfiles来设定当前 context需要使用的环境,在开发中使用@Profile注解类或方法,达到不同情况下选择实例化不同的Bean
- 通过设定jvm的spring.profiles.active参数来设置配置环境
- Web项目设置在Servlet的context paramter中
- 示例
@Configuration
public class ProfileConfig {
@Bean
@Profile("dev")
public ProfileService devService() {
ProfileService profileService = new ProfileService();
profileService.setName("dev");
return profileService;
}
@Bean
@Profile("prod")
public ProfileService prodService() {
ProfileService profileService = new ProfileService();
profileService.setName("prod");
return profileService;
}
}
public class ProfileMain {
public static void main(String[] args) {
// System.setProperty("spring.profiles.active", "dev");
// AnnotationConfigApplicationContext context =
// new AnnotationConfigApplicationContext(ProfileConfig.class);
// ProfileService profileService = context.getBean(ProfileService.class);
// profileService.run();
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("prod");
context.register(ProfileConfig.class);
context.refresh();
ProfileService profileService = context.getBean(ProfileService.class);
profileService.run();
}
}
这里提供了两种办法来设置运行环境