最近在学习spring全注解开发,想和大家分享,spring4以前我们都是编写spring的配置文件进行spring的程序的开发,在Spring4以后引入了全注解模式的开发,去除了编写繁琐的配置文件,使用配置类即可进行bean的扫描和注册。鉴于此,我写了一些最为简单的入门小demo,分享于大家一起学习。整个demo项目如下:
第一步:搭建一个Maven项目;修改pom文件如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>ann_test</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
第二步:创建多个类
1.Car类
package com.test.ann_bean;
public class Car {
}
2.Cat类
package com.test.ann_bean;
public class Cat {
}
3.Color类
package com.test.ann_bean;
public class Color {
}
4.Student类
package com.test.ann_bean;
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student() {
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
5.StudentDao类
package com.test.ann_dao;
import org.springframework.stereotype.Repository;
@Repository
public class StudentDao {
}
6.StudentService类
package com.test.ann_service;
import org.springframework.stereotype.Service;
@Service
public class StudentService {
}
7.配置文件类MainConfig
package com.test.ann_test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import com.sun.prism.paint.Color;
import com.test.ann_bean.Student;
import com.test.ann_service.StudentService;
/**
*
* @Configuration声明此类为配置类
* @ComponentScan(basePackages=("com.test"))扫描包con.test下边的类。
* (扫描带有@Controller/@Service/@Repository/@Component注解的类)
* MainConfig
* UpdateTime:2018年7月24日-下午2:15:50
* @version 1.0.0
*
*/
@Configuration
@ComponentScan(basePackages=("com.test"))
public class MainConfig {
/**
*
* 在Spring容器内注册一个Bean
* MethodName:createStudent
* UpdateTime:2018年7月24日-下午2:23:31
* @return Student
* @exception
* @since 1.0.0
*/
// @Scope("prototype") //默认是单例模式,注意:单例和多例模式其加载bean的时间不同,多例默认懒加载,而单例,默认在容器加载时就注册bean
@Lazy
@Bean("student")
public Student createStudent(){
return new Student("zhangsan",18);
}
}
8.主方法执行类
package com.test.ann_test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
//根据配置类获取spring上下文环境
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
//获取所有在容器中注册的类的名称,并进行打印
String[] beanDefinitionNames = context.getBeanDefinitionNames();
for(String name:beanDefinitionNames){
System.out.println(name);
}
}
}
运行结果如下:
从运行结果我们可以看出,@ComponentScan(basePackages=("com.test"))扫描包con.test下边的类且只扫描带有@Controller/@Service/@Repository/@Component注解的类
此外,我们也可以通过Import导入组件的方式将bean注册到spring容器中
如下:
在配置类添加注解@Import:
package com.test.ann_test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import com.sun.prism.paint.Color;
import com.test.ann_bean.Student;
import com.test.ann_service.StudentService;
/**
*
* @Configuration声明此类为配置类
* @ComponentScan(basePackages=("com.test"))扫描包con.test下边的类。
* (扫描带有@Controller/@Service/@Repository/@Component注解的类)
* @Import 导入组件,参数为数组,其中com.test.ann_bean.Color为全类名,是要注
* 册为bean的类
* MainConfig
* UpdateTime:2018年7月24日-下午2:15:50
* @version 1.0.0
*
*/
@Configuration
@ComponentScan(basePackages=("com.test"))
@Import({com.test.ann_bean.Color.class})
public class MainConfig {
/**
*
* 在Spring容器内注册一个Bean
* MethodName:createStudent
* UpdateTime:2018年7月24日-下午2:23:31
* @return Student
* @exception
* @since 1.0.0
*/
// @Scope("prototype") //默认是单例模式,注意:单例和多例模式其加载bean的时间不同,多例默认懒加载,而单例,默认在容器加载时就注册bean
@Lazy
@Bean("student")
public Student createStudent(){
return new Student("zhangsan",18);
}
}
然后执行主方法,结果如下:
import组件导入还支持ImportSelector实现类的注册:如下,编写一个实现类:
package com.test.ann_test;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
/**
* 组件生成类
* MyImportSelector
* UpdateTime:2018年7月24日-下午2:26:24
* @version 1.0.0
*
*/
public class MyImportSelector implements ImportSelector{
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
// TODO Auto-generated method stub
//返回一个全类名构成的字符串数组。注意:这里不能返回null,可以返回空数组,如果返回null,会在后续执行时出现空指针异常
return new String[]{"com.test.ann_bean.Cat","com.test.ann_bean.Car"};
}
}
修改配置类:
package com.test.ann_test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import com.sun.prism.paint.Color;
import com.test.ann_bean.Student;
import com.test.ann_service.StudentService;
/**
*
* @Configuration声明此类为配置类
* @ComponentScan(basePackages=("com.test"))扫描包con.test下边的类。
* (扫描带有@Controller/@Service/@Repository/@Component注解的类)
* @Import 导入组件,参数为数组,其中com.test.ann_bean.Color为全类名,是要注
* 册为bean的类,MyImportSelector是一个实现ImportSelector的类,返回一个全类
* 名构成的字符串数组
* MainConfig
* UpdateTime:2018年7月24日-下午2:15:50
* @version 1.0.0
*
*/
@Configuration
@ComponentScan(basePackages=("com.test"))
@Import({com.test.ann_bean.Color.class,MyImportSelector.class})
public class MainConfig {
/**
*
* 在Spring容器内注册一个Bean
* MethodName:createStudent
* UpdateTime:2018年7月24日-下午2:23:31
* @return Student
* @exception
* @since 1.0.0
*/
// @Scope("prototype") //默认是单例模式,注意:单例和多例模式其加载bean的时间不同,多例默认懒加载,而单例,默认在容器加载时就注册bean
@Lazy
@Bean("student")
public Student createStudent(){
return new Student("zhangsan",18);
}
}
执行主方法结果如下:
我们可以看出,在此测试中并没有编写任何spring的配置文件,只是加载了一个配置类就实现了bean的注册,是不是很神奇!!!