zoukankan      html  css  js  c++  java
  • 01-spring定义bean的几种方法

    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);
        }
    }
    

    image-20210315132419503

    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对象

    image-20210315132933680

    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);
        }
    }
    

    也在控制台中打印了

    image-20210315133415928

    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);
        }
    }
    

    image-20210315133743379

    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);
    

    然后运行,测试,看控制台结果

    image-20210315135919661

    我们还有另外一种更改的方法就是改为Person类型

    Person user = applicationContext.getBean("user", Person.class);
    

    通过控制台打印结果可以看出

    image-20210315140047289

    我们可以看出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());
        }
    }
    

    通过控制台打印的结果可以看到

    image-20210315141424986

    此时的User对象并不是我们自己写的User类,而是在Supplier定义的User对象

  • 相关阅读:
    bzoj 1015: [JSOI2008]星球大战starwar【并查集】
    bzoj 1026: [SCOI2009]windy数【数位dp】
    bzoj 3231: [Sdoi2008]递归数列【矩阵乘法】
    bzoj 4198: [Noi2015]荷马史诗【哈夫曼树+贪心】
    bzoj 1093: [ZJOI2007]最大半连通子图【tarjan+拓扑排序+dp】
    bzoj 3209: 花神的数论题【数位dp】
    bzoj [JSOI2010]Group 部落划分 Group【二分+并查集】
    bzoj 1087: [SCOI2005]互不侵犯King【状压dp】
    bzoj 2730: [HNOI2012]矿场搭建【tarjan】
    bzoj 1878: [SDOI2009]HH的项链【树状数组】
  • 原文地址:https://www.cnblogs.com/coderD/p/14537418.html
Copyright © 2011-2022 走看看