上一节讲解了ioc对我们的帮助,可以帮助我们创建bean,并用来控制这个bean容器,下面我们说几种创建bean的方式.
bean的创建方式
基于xml的创建方式
一丶创建基于xml的实体类
package com.spring.ioc.bean;
/**
* @program: IOC
* @description: 基于xml的创建bean的方式
* @author: ZhengZhe
* @create: 2019-11-10 09:14
*/
public class XmlBean {
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;
}
@Override
public String toString() {
return "XmlBean{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
二丶创建对应的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="userInfo" class="com.spring.ioc.bean.XmlBean" >
<property name="age" value="24"></property>
<property name="name" value="小哲"></property>
</bean>
</beans>
三丶创建测试类
package com.spring.ioc;
import com.spring.ioc.bean.XmlBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @program: IOC
* @description: 基于xml的创建bean的测试类
* @author: ZhengZhe
* @create: 2019-11-10 09:22
*/
public class XmlBeanTest {
public static void main(String[] args) {
//第一步我们肯定是需要引入我们创建的配置文件
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("xmlBean.xml");
//通过bean的实体类名字获取对应的 实体类
XmlBean xmlBean = (XmlBean) context.getBean("xmlBean");
//直接输出 对应的实体类
System.out.println(xmlBean.toString());
}
}
四丶 输出结果
XmlBean{name='小哲', age=24}
基于@Configuration 和@Bean 注解的创建方式
一丶创建一个配置类用来配置对应的bean
package com.spring.ioc.config;
import com.spring.ioc.bean.UserBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @program: IOC
* @description: 基于@Configuration 和@Bean 注解的创建方式
* @author: ZhengZhe
* @create: 2019-11-10 09:39
*/
@Configuration
public class MyConfig {
@Bean
public UserBean userBean(){
//创建实体类对象
UserBean userBean = new UserBean();
userBean.setName("小哲");
userBean.setAge(25);
return userBean;
}
}
二丶创建实体类
package com.spring.ioc.bean;
/**
* @program: IOC
* @description: 基于@Configuration 和@Bean 注解的创建方式
* @author: ZhengZhe
* @create: 2019-11-10 09:41
*/
public class UserBean {
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;
}
@Override
public String toString() {
return "UserBean{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
三丶创建测试类进行测试
package com.spring.ioc;
import com.spring.ioc.bean.UserBean;
import com.spring.ioc.config.MyConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @program: IOC
* @description: 基于@Configuration 和@Bean 注解的创建方式
* @author: ZhengZhe
* @create: 2019-11-10 09:52
*/
public class MyConfigTest {
public static void main(String[] args) {
//利用注解配置容器类进行加载myconfig配置类
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MyConfig.class);
//通过默认的名字进行对应bean的获取
UserBean userBean = (UserBean) context.getBean("userBean");
//直接输出
System.out.println(userBean.toString());
}
}
三丶输出结果
UserBean{name='小哲', age=25}
基于@Configuration 和@ComponentScan和@Componet、@service 、@controller、@Repository 注解方式
一丶创建实体类
package com.spring.ioc.bean;
import org.springframework.stereotype.Component;
/**
* @program: IOC
* @description: ###### 基于@Configuration 和 @ComponentScan h和@Componet、@service 、@controller、@Repository 注解方式
* @author: ZhengZhe
* @create: 2019-11-10 09:57
*/
@Component//这个注解需要加上
public class UserBean2 {
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;
}
@Override
public String toString() {
return "UserBean{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
二丶创建配置类
package com.spring.ioc.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @program: IOC
* @description: 基于@Configuration 和 @ComponentScan h和@Componet、@service 、@controller、@Repository 注解方式
* @author: ZhengZhe
* @create: 2019-11-10 09:55
*/
@Configuration
@ComponentScan(basePackages = "com.spring.ioc.bean")
public class MyConfig2 {
}
三丶创建测试类
package com.spring.ioc;
import com.spring.ioc.bean.UserBean;
import com.spring.ioc.bean.UserBean2;
import com.spring.ioc.config.MyConfig;
import com.spring.ioc.config.MyConfig2;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @program: IOC
* @description: ###### 基于@Configuration 和 @ComponentScan h和@Componet、@service 、@controller、@Repository 注解方式
* @author: ZhengZhe
* @create: 2019-11-10 10:07
*/
public class MyConfig2Test {
public static void main(String[] args) {
//利用注解配置容器类进行加载myconfig配置类
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MyConfig2.class);
//通过默认的名字进行对应bean的获取
UserBean2 userBean = (UserBean2) context.getBean("userBean2");
userBean.setName("小哲");
userBean.setAge(25);
//直接输出
System.out.println(userBean.toString());
}
}
四丶输出结果
UserBean{name='小哲', age=25}
基于@Configuration 和@Import注解 方式创建bean
注意:此方式创建的bean只能根据对象类型获取bean,无法根据id获取
一丶创建实体类
package com.spring.ioc.bean;
/**
* @program: IOC
* @description: 基于@Configuration 和@Bean 注解的创建方式
* @author: ZhengZhe
* @create: 2019-11-10 09:41
*/
public class UserBean {
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;
}
@Override
public String toString() {
return "UserBean{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
二丶创建配置类
package com.spring.ioc.config;
import com.spring.ioc.bean.UserBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* @program: IOC
* @description: 基于@Configuration 和@Import注解
* @author: ZhengZhe
* @create: 2019-11-10 10:12
*/
@Configuration
@Import(UserBean.class)
public class MyConfig3 {
}
三丶创建测试类
package com.spring.ioc;
import com.spring.ioc.bean.UserBean;
import com.spring.ioc.config.MyConfig;
import com.spring.ioc.config.MyConfig3;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @program: IOC
* @description: 基于@Configuration 和@Import注解
* @author: ZhengZhe
* @create: 2019-11-10 10:13
*/
public class MyConfig3Test {
public static void main(String[] args) {
//利用注解配置容器类进行加载myconfig配置类
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MyConfig3.class);
//通过默认的名字进行对应bean的获取
UserBean userBean = (UserBean) context.getBean(UserBean.class);
userBean.setName("小哲");
userBean.setAge(25);
//直接输出
System.out.println(userBean.toString());
}
}
四丶输出结果
UserBean{name='小哲', age=25}
基于@Configuration 加@Import 和ImportSelector 接口
ImportSelector 接口其主要作用是收集需要导入的配置类,如果该接口的实现类同时实现EnvironmentAware, BeanFactoryAware ,BeanClassLoaderAware或者ResourceLoaderAware,那么在调用其selectImports方法之前先调用上述接口中对应的方法,如果需要在所有的@Configuration处理完在导入时可以实现DeferredImportSelector接口
一丶创建实体类
package com.spring.ioc.bean;
/**
* @program: IOC
* @description: 基于@Configuration 加@Import 和ImportSelector 接口
* @author: ZhengZhe
* @create: 2019-11-10 15:53
*/
public class UserBean4 {
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;
}
@Override
public String toString() {
return "UserBean{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
二丶创建ImportSelector 的实现类
package com.spring.ioc.Impl;
import com.spring.ioc.bean.UserBean;
import com.spring.ioc.bean.UserBean4;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;
/**
* @program: IOC
* @description: 基于@Configuration 加@Import 和ImportSelector 接口
* @author: ZhengZhe
* @create: 2019-11-10 15:51
*/
public class MyImportSelector implements ImportSelector, EnvironmentAware {
private Environment environment;
public String[] selectImports(AnnotationMetadata annotationMetadata) {
//当配置文件包含test属性时候创建对象
if( environment.containsProperty("test")){
return new String[]{UserBean4.class.getName()};
}
return null;
}
public void setEnvironment(Environment environment) {
this.environment=environment;
}
}
三丶创建配置文件properties
test=小哲
四丶创建配置类
package com.spring.ioc.config;
import com.spring.ioc.Impl.MyImportSelector;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
/**
* @program: IOC
* @description: 基于@Configuration 加@Import 和ImportSelector 接口
* @author: ZhengZhe
* @create: 2019-11-10 15:50
*/
@Configuration
@Import(MyImportSelector.class)
@PropertySource("application4.properties")
public class MyConfig4 {
}
五丶 创建测试类
package com.spring.ioc;
import com.spring.ioc.bean.UserBean;
import com.spring.ioc.bean.UserBean4;
import com.spring.ioc.config.MyConfig3;
import com.spring.ioc.config.MyConfig4;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @program: IOC
* @description: 基于@Configuration 加@Import 和ImportSelector 接口
* @author: ZhengZhe
* @create: 2019-11-10 15:54
*/
public class MyConfig4Test {
public static void main(String[] args) {
//利用注解配置容器类进行加载myconfig配置类
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MyConfig4.class);
//通过默认的名字进行对应bean的获取
UserBean4 userBean = (UserBean4) context.getBean(UserBean4.class);
userBean.setName("小哲");
userBean.setAge(25);
//直接输出
System.out.println(userBean.toString());
}
}
六丶输出结果
UserBean{name='小哲', age=25}
基于@Configuration 加@Import 和ImportBeanDefinitionRegistrar接口
一丶创建实体类
package com.spring.ioc.bean;
/**
* @program: IOC
* @description: 基于@Configuration 加@Import 和ImportBeanDefinitionRegistrar接口
* @author: ZhengZhe
* @create: 2019-11-10 16:00
*/
public class UserBean5 {
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;
}
@Override
public String toString() {
return "UserBean{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
二丶创建 实现类
package com.spring.ioc.Impl;
import com.spring.ioc.bean.UserBean5;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
/**
* @program: IOC
* @description: 基于@Configuration 加@Import 和ImportBeanDefinitionRegistrar接口
* @author: ZhengZhe
* @create: 2019-11-10 16:00
*/
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
//当容器中包含某个类时加载另一个类
if (beanDefinitionRegistry.containsBeanDefinition("userBean4")) {
RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();
rootBeanDefinition.setBeanClass(UserBean5.class);
beanDefinitionRegistry.registerBeanDefinition("userBean5", rootBeanDefinition);
}
}
}
三丶创建 配置类
package com.spring.ioc.config;
import com.spring.ioc.Impl.MyImportBeanDefinitionRegistrar;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* @program: IOC
* @description: 基于@Configuration 加@Import 和ImportBeanDefinitionRegistrar接口
* @author: ZhengZhe
* @create: 2019-11-10 16:01
*/
@Configuration
@Import(MyImportBeanDefinitionRegistrar.class)
@ComponentScan(basePackages = "com.spring.ioc.bean")
public class MyConfig5 {
}
四丶创建测试类
package com.spring.ioc;
import com.spring.ioc.bean.UserBean4;
import com.spring.ioc.bean.UserBean5;
import com.spring.ioc.config.MyConfig4;
import com.spring.ioc.config.MyConfig5;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @program: IOC
* @description: 基于@Configuration 加@Import 和ImportBeanDefinitionRegistrar接口
* @author: ZhengZhe
* @create: 2019-11-10 16:04
*/
public class MyConfig5Test {
public static void main(String[] args) {
//利用注解配置容器类进行加载myconfig配置类
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MyConfig5.class);
//通过默认的名字进行对应bean的获取
UserBean5 userBean = (UserBean5) context.getBean("userBean5");
userBean.setName("小哲");
userBean.setAge(25);
//直接输出
System.out.println(userBean.toString());
}
}
五丶输出结果
UserBean{name='小哲', age=25}
实现FactoryBean接口 + @Component
注意: 此方式通过factoryBean 名称 获取的实际是UserInfo 对象, MyFactoryBean此对象要通过beanFactory的方式获取,要获取MyFactoryBean 需要在 factoryBean 前面加&符号。
一丶创建实体类
package com.spring.ioc.bean;
/**
* @program: IOC
* @description: 实现FactoryBean接口 + @Component
* @author: ZhengZhe
* @create: 2019-11-10 16:10
*/
public class UserBean6 {
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;
}
@Override
public String toString() {
return "UserBean{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
创建实现类
package com.spring.ioc.Impl;
import com.spring.ioc.bean.UserBean6;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
/**
* @program: IOC
* @description: 实现FactoryBean接口 + @Component
* @author: ZhengZhe
* @create: 2019-11-10 16:13
*/
@Component
public class MyFactoryBean implements FactoryBean {
@Nullable
public Object getObject() throws Exception {
return new UserBean6();
}
@Nullable
public Class<?> getObjectType() {
return UserBean6.class;
}
}
三丶创建配置类
package com.spring.ioc.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @program: IOC
* @description: 实现FactoryBean接口 + @Component
* @author: ZhengZhe
* @create: 2019-11-10 16:11
*/
@Configuration
@ComponentScan(basePackages = "com.spring.ioc.bean")
public class MyConfig6 {
}
四丶创建测试类
package com.spring.ioc;
import com.spring.ioc.Impl.MyFactoryBean;
import com.spring.ioc.bean.UserBean5;
import com.spring.ioc.bean.UserBean6;
import com.spring.ioc.config.MyConfig5;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @program: IOC
* @description: 实现FactoryBean接口 + @Component
* @author: ZhengZhe
* @create: 2019-11-10 16:04
*/
public class MyConfig6Test {
public static void main(String[] args) {
//利用注解配置容器类进行加载myconfig配置类
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MyConfig5.class);
//通过默认的名字进行对应bean的获取
UserBean6 userBean = (UserBean6) context.getBean("myFactoryBean");
userBean.setName("小哲");
userBean.setAge(25);
//直接输出
System.out.println(userBean.toString());
MyFactoryBean myFactoryBean=(MyFactoryBean)context.getBean("&myFactoryBean");
System.out.println(myFactoryBean.toString());
}
}
五丶输出结果
UserBean{name='小哲', age=25}
@Conditional 和Condition接口
Conditional 通常作用于类或方法上,当满足某种条件则实例化对应的类或执行对应的方法。
一丶创建实体类
package com.spring.ioc.bean;
/**
* @program: IOC
* @description: @Conditional 和Condition接口
* @author: ZhengZhe
* @create: 2019-11-10 16:19
*/
public class UserBean7 {
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;
}
@Override
public String toString() {
return "UserBean{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
二丶创建实现类
package com.spring.ioc.Impl;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
/**
* @program: IOC
* @description: @Conditional 和Condition接口
* @author: ZhengZhe
* @create: 2019-11-10 16:20
*/
public class MyCondition implements Condition {
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
//配置文件包含test属性则返回tue
if(conditionContext.getEnvironment().containsProperty("test")){
return true;
}
return false;
}
}
三丶创建配置文件
test=小哲
四丶创建配置类
package com.spring.ioc.config;
import com.spring.ioc.Impl.MyCondition;
import com.spring.ioc.bean.UserBean7;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
/**
* @program: IOC
* @description: @Conditional 和Condition接口
* @author: ZhengZhe
* @create: 2019-11-10 16:21
*/
@Configuration
@PropertySource("application.properties")
public class MyConfig7 {
@Bean
@Conditional(value = {MyCondition.class})
public UserBean7 userBean7(){
UserBean7 userBean7=new UserBean7();
userBean7.setName("小哲");
return userBean7;
};
}
五丶创建测试类
package com.spring.ioc;
import com.spring.ioc.Impl.MyFactoryBean;
import com.spring.ioc.bean.UserBean6;
import com.spring.ioc.bean.UserBean7;
import com.spring.ioc.config.MyConfig5;
import com.spring.ioc.config.MyConfig7;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @program: IOC
* @description: @Conditional 和Condition接口
* @author: ZhengZhe
* @create: 2019-11-10 16:04
*/
public class MyConfig7Test {
public static void main(String[] args) {
//利用注解配置容器类进行加载myconfig配置类
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MyConfig7.class);
//通过默认的名字进行对应bean的获取
UserBean7 userBean7=(UserBean7)context.getBean("userBean7");
}
}
六丶输出结果
UserBean{name='小哲', age=25}