SpringBoot Bean 创建方式 XML + Annotation

本文详细介绍了Spring框架中创建Bean的多种方式,包括通过XML、注解和工厂方法等,并提供了具体的代码示例,深入解析了每种创建方式的实现原理。

Spring创建bean方式多种,直接上代码:

  • 通过XML创建Bean有三种方式:默认无参构造,静态工厂,实例工厂
  • 通过Annotation创建Bean有三种方式:注解配置方式,获取配置类中使用Bean注解方式,直接使用注解配置注册Bean,通过扫描指定包路径下通过注解声明的Bean实例。
package com.example.demo;

import com.example.demo.bean.UserService;
import com.example.demo.bean.annotation.BeanAnnotationUserService;
import com.example.demo.bean.annotation.BeanConfig;
import com.example.demo.bean.annotation.ServiceAnnotationUserService;
import com.example.demo.bean.xml.BeanFactoryUserService;
import com.example.demo.bean.xml.BeanInstanceFactoryUserService;
import com.example.demo.bean.xml.BeanXmlUserServcie;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.annotation.Resource;

@SpringBootTest
class DemoApplicationTests {

	@Test
	void testClassPathXmlApplicationContext() {
		ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
		UserService userServcie = (UserService)context.getBean("beanXmlUserServcie");
		System.out.println(userServcie.toString());
	}

	@Test
	void testFactoryClassPathXmlApplicationContext() {
		ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
		UserService userServcie = (UserService)context.getBean("beanFactoryUserService");
		System.out.println(userServcie.toString());
	}

	@Test
	void testInsFactoryClassPathXmlApplicationContext() {
		ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
		UserService userServcie = (UserService)context.getBean("beanInstanceFactoryUserService");
		System.out.println(userServcie.toString());
	}

	@Test
	void testAnnotationConfigApplicationContext() {
		ApplicationContext context = new AnnotationConfigApplicationContext(BeanAnnotationUserService.class);
		UserService userServcie = (UserService)context.getBean("beanAnnotationUserService");
		System.out.println(userServcie.toString());
	}

	@Test
	void testBeanConfigAnnotationConfigApplicationContext() {
		ApplicationContext context = new AnnotationConfigApplicationContext(BeanConfig.class);
		UserService userServcie = (UserService)context.getBean("beanAnnotationUserServiceConfig");
		System.out.println(userServcie.toString());
	}

	@Test
	void testServiceAnnotationConfigApplicationContext() {
		ApplicationContext context = new AnnotationConfigApplicationContext("com.example.demo.bean.annotation");
		UserService userServcie = (UserService)context.getBean("serviceAnnotationUserService");
		System.out.println(userServcie.toString());
	}
}

 

创建一个UserService空接口,方便测试Bean对象获取。

package com.example.demo.bean;

public interface UserService {
}

通过XML获取Bean对象BeanXmlUserServcie 

<?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="beanXmlUserServcie" class="com.example.demo.bean.xml.BeanXmlUserServcie"></bean>
</beans>
package com.example.demo.bean.xml;

import com.example.demo.bean.UserService;

/**
 * @author wangSong
 */
public class BeanXmlUserServcie implements UserService {
    @Override
    public String toString() {
        return "this bean is create by Spring bean Xml";
    }
}

通过静态工厂获取Bean实例对象 BeanFactoryUserService

<?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="beanFactoryUserService" class="com.example.demo.bean.xml.StaticFactory" factory-method="createUserService"></bean>
</beans>
package com.example.demo.bean.xml;

import com.example.demo.bean.UserService;

public class StaticFactory {

    public static UserService  createUserService(){
        return new BeanFactoryUserService();
    }
}

package com.example.demo.bean.xml;

import com.example.demo.bean.UserService;

public class BeanFactoryUserService implements UserService {
    @Override
    public String toString() {
        return "this bean is create by Spring Bean Static Factory Xml";
    }
}

通过实例工厂获取Bean对象BeanInstanceFactoryUserService

<?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="instanceFactory" class="com.example.demo.bean.xml.InstanceFactory"></bean>
    <bean id="beanInstanceFactoryUserService" factory-bean="instanceFactory" factory-method="createUserService"></bean>

</beans>
package com.example.demo.bean.xml;

import com.example.demo.bean.UserService;

public class InstanceFactory {

    public  UserService  createUserService(){
        return new BeanInstanceFactoryUserService();
    }
}

package com.example.demo.bean.xml;

import com.example.demo.bean.UserService;

public class BeanInstanceFactoryUserService implements UserService {
    @Override
    public String toString() {
        return "this bean is create by Spring Bean Instance Factory Xml";
    }
}

通过注解配置获取配置的Bean实例,也可以直接获取指定class对象Bean实例。

package com.example.demo.bean.annotation;

import com.example.demo.bean.UserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanConfig {

    @Bean(name="beanAnnotationUserServiceConfig")
   public UserService getBeanUserService(){
       return new BeanAnnotationUserService();
   }
}

通过扫描包下注解类,获取Bean实例

package com.example.demo.bean.annotation;

import com.example.demo.bean.UserService;
import org.springframework.stereotype.Service;

@Service(value="serviceAnnotationUserService")
public class ServiceAnnotationUserService implements UserService {
    @Override
    public String toString() {
        return "this bean is create by Spring Service Annotation ";
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

仰望星空@脚踏实地

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值