Spring注解

编程

bean生产

@Bean

@Configuration // 配置类==配置文件,告诉Spring这是一个配置类 

public class MainConfig {

// 给容器注册一个Bean;类型为返回值的类型,id默认用方法名作为id

@Bean("PersonX")

public Person person(){

return new Person("lisi",20);

}

}

    @Test

public void test01(){

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);

Person person1 = applicationContext.getBean(Person.class);

System.out.println(person1);

Person person2 = (Person) applicationContext.getBean("PersonX");

System.out.println(person2);

}

@ComponentScan

  1. 包扫描,只要标注了@Controller、@Service、@Repository、@Component都会被扫描加进容器,bean的id为类名首字母小写

@Configuration

@ComponentScan(value = "cn.ccb")

public class MainConfig {

}

@Test

public void test02(){

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);

String[] names = applicationContext.getBeanDefinitionNames();

for (String name : names) {

System.out.println(name);

}

}

结果:

mainConfig

bookController

bookDao

bookService

  1. 包扫描,排除 excludeFilters

@Configuration

@ComponentScan(value = "cn.ccb",excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes={Controller.class,Service.class})})

public class MainConfig {

}

@Test

public void test02(){

// 将上面test02()代码粘贴在此处

}

结果:

mainConfig

bookDao

  1. 包扫描,包括 includeFilters

@Configuration

@ComponentScan(value = "cn.ccb",includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes={Controller.class,Service.class})},useDefaultFilters=false)

public class MainConfig {

}

@Test

public void test02(){

// 将上面test02()代码粘贴在此处

}

结果:

mainConfig

bookController

bookService

@Scope

@Configuration

public class MainConfig {

// singleton:单实例的(默认值),ioc容器启动就会调用方法创建对象放到ioc容器中

// prototype:多实例的,ioc容器启动并不会调用方法创建对象放在容器中,每次获取的时候才会调用方法创建对象

@Scope("prototype")

@Bean

public Person person(){

return new Person("张三",22);

}

}

@Import

@Configuration

@Import({Color.class,Red.class}) //导入组件,id默认是组件的全类名

public class MainConfig {

}

结果:

cn.ccb.entities.Color

cn.ccb.entities.Red

bean赋值

@Value

@Data

@AllArgsConstructor

@NoArgsConstructor

public class Person {

@Value("四川妹子")

private String name;

@Value("1234")

private int age;

}

@Configuration

public class MainConfigOfPropertyValues {

@Bean

public Person person(){

return new Person();

}

}

@Test

public void test1(){

AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);

Object person = applicationContext.getBean("person");

System.out.println(person);

}

结果:

Person(name=四川妹子, age=1234)

@Autowired

@Service

public class BookService {

/**

* @Autowired:自动注入

* 1、默认按照类型去容器中找对应组件:applicationContext.getBean(BookController.class);

* 2、如果有多个相同类型的组件,再将变量名作为组件的id去容器中查找

* @Qualifier:指定需要装配的组件的id

*/

@Autowired

private BookController bookController;

}

@Resource

@Service

public class BookService {

/**

* @Resource可以和@Autowired一样实现自动装配功能,默认按照变量名作为组件的id去容器中查找

* 不支持@Primary功能和@Autowired(required=false)

* @Autowired:spring定义的,@Resource是java规范

*/

@Resource(name = "InnerConfig")

private BookController bookController;

}

FactoryBean

// 创建一个FactoryBean

public class ColorFactoryBean implements FactoryBean<Color> {

public Color getObject() throws Exception {

System.out.println("调用了----------");

return new Color();

}

public Class<?> getObjectType() {

return Color.class;

}

public boolean isSingleton() {

return false;

}

}

@Configuration

public class MainConfig {

@Bean

public ColorFactoryBean colorFactoryBean(){

return new ColorFactoryBean();

}

}

@Test

public void test03(){

/**

* Spring提供的FactoryBean:

* 默认获取到的是工厂Bean调用getObject()创建的对象

* 要获取工厂Bean本身,需要在bean的id前加一个& ---> &colorFactoryBean

*/

AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);

Object bean = applicationContext.getBean("colorFactoryBean");

System.out.println(bean.getClass()); // class cn.ccb.entities.Color

Object bean2 = applicationContext.getBean("&colorFactoryBean");

System.out.println(bean2.getClass()); // class cn.ccb.entities.ColorFactoryBean

}

以上是 Spring注解 的全部内容, 来源链接: utcz.com/z/519128.html

回到顶部