01-spring定义bean的几种方法
1、<bean/ >方法
首先我们创建一个User对象,并且实现它的getter和setter方法
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
然后我们在resource下创建一个spring.xml的配置文件,然后在spring.xml文件中添加
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="nuc.edu.User"/>
</beans>
然后编写测试方法,在控制台打印输出
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring" +
".xml");
User user = classPathXmlApplicationContext.getBean("user", User.class);
System.out.println(user);
}
}
2、@Bean
首先我们创建一个Config.java类,然后注入@Bean
public class Config {
@Bean
public User user(){
return new User();
}
}
然后编写测试类方法
public class Main {
public static void main(String[] args) {
//ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring" +
// ".xml");
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
User user = applicationContext.getBean("user", User.class);
System.out.println(user);
}
}
同样我们在控制台获得了User对象
3、@Component
我们将Config.class类里的方法注释掉,然后添加@ComponentScan("nuc.edu"),把Config.class类交由spring容器管理
@ComponentScan("nuc.edu")
public class Config {
// @Bean
// public User user(){
// return new User();
// }
}
然后我们在User类中添加@ComponentScan注解
@Component
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
最后我们编写测试
public class Main {
public static void main(String[] args) {
//ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring" +
// ".xml");
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
User user = applicationContext.getBean("user", User.class);
System.out.println(user);
}
}
也在控制台中打印了
4、BeanDefinition
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
beanDefinition.setBeanClass(User.class);
applicationContext.registerBeanDefinition("user",beanDefinition);
applicationContext.refresh();
User user = applicationContext.getBean("user", User.class);
System.out.println(user);
}
}
5、通过FactoryBean间接的定义一个Bean
首先我们创建一个Person对象
public class Person {
}
然后我们创建一个NucFactoryBean类并且实现FactoryBean,并且对它的方法进行重写
public class NucFactoryBean implements FactoryBean {
@Override
public Object getObject() throws Exception {
Person person = new Person();
System.out.println(person);
return person;
}
@Override
public Class<?> getObjectType() {
return Person.class;
}
}
然后我们再写主类方法
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
beanDefinition.setBeanClass(NucFactoryBean.class);
applicationContext.registerBeanDefinition("user",beanDefinition);
applicationContext.refresh();
User user = applicationContext.getBean("user", User.class);
System.out.println(user);
}
}
运行代码出错:
提示我们期望的应该是nu.edu.User类,但是实际上的确是nuc.edu.Person类
其实:
&user :NucFactoryBean类型的对象
user:person类型的对象
我们需要将代码进行修改,主需要修改一行代码即可
NucFactoryBean user = applicationContext.getBean("&user", NucFactoryBean.class);
然后运行,测试,看控制台结果
我们还有另外一种更改的方法就是改为Person类型
Person user = applicationContext.getBean("user", Person.class);
通过控制台打印结果可以看出
我们可以看出applicationContext.getBean()得到的Person对象和NucFactoryBean里new出来的对象是同一个对象
6、通过Supplier定义Bean对象
示例代码
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.registerBean(User.class, new Supplier<User>() {
@Override
public User get() {
User user = new User();
user.setName("zhangsan");
return user;
}
});
applicationContext.refresh();
User user = applicationContext.getBean("user", User.class);
System.out.println(user.getName());
}
}
通过控制台打印的结果可以看到
此时的User对象并不是我们自己写的User类,而是在Supplier定义的User对象