你知道几种定义bean的方式

2022-10-25 17:13:22 浏览数 (1)

文章目录
  • 你知道几种定义bean的方式
    • 1.通过xml配置文件
    • 2. 通过注解@Bean
    • 3. 通过注解@Component
    • 4. 通过BeanDefinition
    • 5. 通过FactoryBean间接定义一个bean
    • 6. 通过supplier

你知道几种定义bean的方式

1.通过xml配置文件

引入包

代码语言:javascript复制
	<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
    </dependencies>
代码语言:javascript复制
package com.xiepanpan;

import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * <p>Description: </p>
 * <p>Copyright: Copyright (c)2,020</p>
 * <p>Company: tope</p>
 * <P>Created Date :2021/5/19</P>
 *
 * @author xiepanpan
 * @version 1.0
 */
public class Main {

    public static void main(String[] args) {
        //通过配置文件
       ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        User user = applicationContext.getBean("user",User.class);
        System.out.println(user);
    }
}

resources 下spring.xml

代码语言:javascript复制
<?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="com.xiepanpan.User"></bean>
</beans>

实体类User

代码语言:javascript复制
package com.xiepanpan;

/**
 * <p>Description: </p>
 * <p>Copyright: Copyright (c)2,020</p>
 * <p>Company: tope</p>
 * <P>Created Date :2021/5/19</P>
 *
 * @author xiepanpan
 * @version 1.0
 */
public class User {
}
2. 通过注解@Bean
代码语言:javascript复制
package com.xiepanpan;

import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * <p>Description: </p>
 * <p>Copyright: Copyright (c)2,020</p>
 * <p>Company: tope</p>
 * <P>Created Date :2021/5/19</P>
 *
 * @author xiepanpan
 * @version 1.0
 */
public class Main {

    public static void main(String[] args) {
        //通过注解
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
        User user = applicationContext.getBean("user",User.class);
        System.out.println(user);
    }
}

Config类

代码语言:javascript复制
package com.xiepanpan;

import org.springframework.context.annotation.Bean;

/**
 * <p>Description: </p>
 * <p>Copyright: Copyright (c)2,020</p>
 * <p>Company: tope</p>
 * <P>Created Date :2021/5/19</P>
 *
 * @author xiepanpan
 * @version 1.0
 */
public class Config {

    @Bean
    public User user() {
        return new User();
    }
}
3. 通过注解@Component
代码语言:javascript复制
package com.xiepanpan;

import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * <p>Description: </p>
 * <p>Copyright: Copyright (c)2,020</p>
 * <p>Company: tope</p>
 * <P>Created Date :2021/5/19</P>
 *
 * @author xiepanpan
 * @version 1.0
 */
public class Main {

    public static void main(String[] args) {
        //通过注解
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
        User user = applicationContext.getBean("user",User.class);
        System.out.println(user);
    }
}

Config类

代码语言:javascript复制
package com.xiepanpan;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

/**
 * <p>Description: </p>
 * <p>Copyright: Copyright (c)2,020</p>
 * <p>Company: tope</p>
 * <P>Created Date :2021/5/19</P>
 *
 * @author xiepanpan
 * @version 1.0
 */
@ComponentScan("com.xiepanpan")
public class Config {
}

User类

代码语言:javascript复制
package com.xiepanpan;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * <p>Description: </p>
 * <p>Copyright: Copyright (c)2,020</p>
 * <p>Company: tope</p>
 * <P>Created Date :2021/5/19</P>
 *
 * @author xiepanpan
 * @version 1.0
 */
@Component
public class User {
}
4. 通过BeanDefinition
代码语言:javascript复制
package com.xiepanpan;

import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * <p>Description: </p>
 * <p>Copyright: Copyright (c)2,020</p>
 * <p>Company: tope</p>
 * <P>Created Date :2021/5/19</P>
 *
 * @author xiepanpan
 * @version 1.0
 */
public class Main {

    public static void main(String[] args) {
        //通过注解
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        //通过BeanDefinition
        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);
    }
}

User类

代码语言:javascript复制
package com.xiepanpan;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * <p>Description: </p>
 * <p>Copyright: Copyright (c)2,020</p>
 * <p>Company: tope</p>
 * <P>Created Date :2021/5/19</P>
 *
 * @author xiepanpan
 * @version 1.0
 */
public class User {
}
5. 通过FactoryBean间接定义一个bean
代码语言:javascript复制
package com.xiepanpan;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;

/**
 * <p>Description: </p>
 * <p>Copyright: Copyright (c)2,020</p>
 * <p>Company: tope</p>
 * <P>Created Date :2021/5/19</P>
 *
 * @author xiepanpan
 * @version 1.0
 */
@Component("user")
public class XppFactoryBean implements FactoryBean {

    /**
     * 返回一个对象
     * @return
     * @throws Exception
     */
    @Override
    public Object getObject() throws Exception {
        return new Person();
    }

    /**
     * 返回对象的类型
     * @return
     */
    @Override
    public Class<?> getObjectType() {
        return Person.class;
    }
}
代码语言:javascript复制
package com.xiepanpan;

import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * <p>Description: </p>
 * <p>Copyright: Copyright (c)2,020</p>
 * <p>Company: tope</p>
 * <P>Created Date :2021/5/19</P>
 *
 * @author xiepanpan
 * @version 1.0
 */
public class Main {

    public static void main(String[] args) 

        //通过注解
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
        Person user = applicationContext.getBean("user",Person.class);
        System.out.println(user);
        XppFactoryBean xppFactoryBean = applicationContext.getBean("&user",XppFactoryBean.class);
        System.out.println(xppFactoryBean);
    }
}
代码语言:javascript复制
package com.xiepanpan;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

/**
 * <p>Description: </p>
 * <p>Copyright: Copyright (c)2,020</p>
 * <p>Company: tope</p>
 * <P>Created Date :2021/5/19</P>
 *
 * @author xiepanpan
 * @version 1.0
 */
@ComponentScan("com.xiepanpan")
public class Config {
}
6. 通过supplier
代码语言:javascript复制
package com.xiepanpan;

import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.function.Supplier;

/**
 * <p>Description: </p>
 * <p>Copyright: Copyright (c)2,020</p>
 * <p>Company: tope</p>
 * <P>Created Date :2021/5/19</P>
 *
 * @author xiepanpan
 * @version 1.0
 */
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("芝姐520");
                return user;
            }
        });
        applicationContext.refresh();
        User user = applicationContext.getBean("user",User.class);
        System.out.println(user.getName());

    }
}
代码语言:javascript复制
package com.xiepanpan;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * <p>Description: </p>
 * <p>Copyright: Copyright (c)2,020</p>
 * <p>Company: tope</p>
 * <P>Created Date :2021/5/19</P>
 *
 * @author xiepanpan
 * @version 1.0
 */
public class User {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

0 人点赞