你知道几种定义bean的方式

你知道几种定义bean的方式

1.通过xml配置文件

引入包

	<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
    </dependencies>
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

<?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

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
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类

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
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类

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类

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
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类

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

    }
}

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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值