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

被折叠的 条评论
为什么被折叠?



