SpringBoot Bean 創建方式 XML + Annotation

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章