声明式
1. <bean/> 使用xml的方式定义bean
1)先引入spring的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
2)定义对象User
package com.frank.oneBean;
/**
* @author 小石潭记
* @date 2021/6/11 21:54
* @Description: ${todo}
*/
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3)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">
<!--scope="prototype" 多例-->
<!--scope="singleton" 单例(默认)-->
<bean id="user" class="com.frank.oneBean.User" scope="singleton">
<property name="name" value="spring bean"/>
</bean>
</beans>
4)使用
ClassPathXmlApplicationContext读取xml的方式获取bean
package com.frank.oneBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author 小石潭记
* @date 2021/6/11 21:55
* @Description: ${todo} 该package主要介绍javaBean springBean
*/
public class Main {
public static void main(String[] args) {
User user = new User(); // 对象
user.setName("java bean");
String name = user.getName();// 满足规范 private get set javaBean
System.out.println(name);
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
User user1 = applicationContext.getBean("user", User.class); // springBean
System.out.println(user1.getName());
}
}
2. @Bean
package com.frank.twoBeanMehtod;
import org.springframework.context.annotation.Bean;
/**
* @author 小石潭记
* @date 2021/6/11 22:06
* @Description: ${todo}
*/
public class Config {
/**
* 通过@Bean注册bean
* @return
*/
@Bean
public User user() {
return new User();
}
}
package com.frank.twoBeanMehtod;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author 小石潭记
* @date 2021/6/11 21:55
* @Description: ${todo} 该package主要介绍 通过config配置bean
*/
public class Main {
public static void main(String[] args) {
// 注解的方式
AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext(Config.class);
User user = configApplicationContext.getBean("user", User.class);
System.out.println(user);
}
}
3.@Component 扫描指定的包下的@Component的注解类
package com.frank.twoBeanMehtod;
import org.springframework.context.annotation.ComponentScan;
/**
* @author 小石潭记
* @date 2021/6/11 22:06
* @Description: ${todo}
*/
@ComponentScan("com.frank") // 注释下面的@Bean 在user对象上添加@Component
public class Config {
}
在User上面添加注解@Component
package com.frank.twoBeanMehtod;
import org.springframework.stereotype.Component;
/**
* @author 小石潭记
* @date 2021/6/11 21:54
* @Description: ${todo}
*/
@Component
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.frank.twoBeanMehtod;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author 小石潭记
* @date 2021/6/11 21:55
* @Description: ${todo} 该package主要介绍 通过config配置bean
*/
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext(Config.class);
User user = configApplicationContext.getBean("user", User.class);
System.out.println(user);
}
}
编程式
4. BeanDefinition,删除user上面的@Component和config上面的@ComponentScan("com.frank") 然后通过
BeanDefinition注册bean
package com.frank.twoBeanMehtod;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author 小石潭记
* @date 2021/6/11 21:55
* @Description: ${todo} 该package主要介绍 通过beanDefinition配置bean
*/
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext();
// 使用beanDefinition定义bean
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
beanDefinition.setBeanClass(User.class);
configApplicationContext.registerBeanDefinition("user", beanDefinition);
configApplicationContext.refresh();
User user = configApplicationContext.getBean("user", User.class);
System.out.println(user);
}
}
5.使用BeanFctory定义bean
package com.frank.twoBeanMehtod;
/**
* @author 小石潭记
* @date 2021/6/12 11:32
* @Description: ${todo}
*/
public class Person {
}
package com.frank.twoBeanMehtod;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;
/**
* @author 小石潭记
* @date 2021/6/12 11:31
* @Description: ${todo}
*/
@Component("person")
public class FrankFactoryBean implements FactoryBean {
public Object getObject() throws Exception {
Person person = new Person();
return person;
}
public Class<?> getObjectType() {
return Person.class;
}
}
AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext(Config.class);
Person person = configApplicationContext.getBean("person", Person.class);
System.out.println(person);
6.使用Supplier创建bean
AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext();
/*configApplicationContext.registerBean(User.class, new Supplier<User>() {
public User get() {
User user = new User();
user.setName("supplier-user");
return user;
}
});*/
configApplicationContext.registerBean(User.class, () -> {
User user = new User();
user.setName("supplier-user");
return user;
});
// 注册之后需要刷新 才会获取到bean
configApplicationContext.refresh();
User user = configApplicationContext.getBean("user", User.class);
System.out.println(user.getName());