Spring Bean有三種配置方式:
- 傳統的XML配置方式
- 基於注解的配置
- 基於類的Java Config
添加spring的maven repository
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <!--這個jar文件包含Spring框架基本的核心工具類,Spring其它組件要都要使用到這個包里的類,是其它組件的基本核心 --> <version>4.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <!--這個jar文件為Spring核心提供了大量擴展 --> <version>4.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <!--對JUNIT等測試框架的簡單封裝 --> <version>4.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <!--為JDBC、Hibernate、JDO、JPA等提供的一致的聲明式和編程式事務管理。--> <version>4.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <!--這個jar文件是所有應用都要用到的,它包含訪問配置文件、創建和管理bean以及進行(IoC/DI)操作相關的所有類 --> <version>4.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <!--這個jar文件包含對Spring對JDBC數據訪問進行封裝的所有類。 --> <version>4.1.0.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency>
一、傳統的XML配置方式
BeanFactory.java
package com.stonegeek.service; /** * Created by StoneGeek on 2018/5/13. */ public interface BeanFactory { public void Beantest(); }
BeanFactoryImpl.java
package com.stonegeek.service.impl; import com.stonegeek.service.BeanFactory; /** * Created by StoneGeek on 2018/5/13. */ public class BeanFactroyImpl implements BeanFactory { @Override public void Beantest() { System.out.println("----------------This is a 傳統的XML配置的bean!-------------------"); } }
applicationContext.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <bean id="beanFactroy" class="com.stonegeek.service.impl.BeanFactroyImpl" /> </beans>
TestBean1.java
package com.stonegeek; import com.stonegeek.service.BeanFactory; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by StoneGeek on 2018/5/13. */ public class TestBean1 { @Test public void test(){ ApplicationContext ctx= new ClassPathXmlApplicationContext("applicationContext.xml"); BeanFactory beanFactory=(BeanFactory) ctx.getBean("beanFactroy"); beanFactory.Beantest(); //----------------This is a 傳統的XML配置的bean!------------------- } }
二、基於java注解的配置
如果一個類使用了@Service,那么此類將自動注冊成一個bean,不需要再在applicationContext.xml文件定義bean了,類似的還包括@Component、@Repository、@Controller。
然后需要在applicationContext.xml文件中加一行,作用是自動掃描base-package包下的注解:
<context:component-scan base-package="com.stonegeek" />
BeanFactoryImpl.java
package com.stonegeek.service.impl; import com.stonegeek.service.BeanFactory; import org.springframework.stereotype.Service; /** * Created by StoneGeek on 2018/5/13. */ @Service("beanFactory") public class BeanFactroyImpl implements BeanFactory { @Override public void Beantest() { System.out.println("----------------This is a 基於Java注解的bean!-------------------"); } }
applicationContext.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <context:component-scan base-package="com.stonegeek" /> </beans>
TestBean2.java
package com.stonegeek; import com.stonegeek.service.BeanFactory; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by StoneGeek on 2018/5/13. */ public class TestBean2 { @Test public void test(){ ApplicationContext ctx= new ClassPathXmlApplicationContext("applicationContext.xml"); BeanFactory beanFactory=(BeanFactory) ctx.getBean("beanFactory"); beanFactory.Beantest(); //This is a 基於java注解的bean! } }
三、基於類的Java Config
通過java類定義spring配置元數據,且直接消除xml配置文件
Spring3.0基於java的配置直接支持下面的注解:
@Configuration
@Bean
@DependsOn
@Primary
@Lazy
@Import
@ImportResource
@Value
BeanFactoryImpl.java
package com.stonegeek.service.impl; import com.stonegeek.service.BeanFactory; /** * Created by StoneGeek on 2018/5/13. */ public class BeanFactoryImpl implements BeanFactory { @Override public void Beantest() { System.out.println("----------------This is a 基於類的Java Config的bean!-------------------"); } }
BeanConfig.java
package com.stonegeek.service.config; import com.stonegeek.service.BeanFactory; import com.stonegeek.service.impl.BeanFactoryImpl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Created by StoneGeek on 2018/5/13. */ @Configuration public class BeanConfig { @Bean public BeanFactory beanFactory(){ return new BeanFactoryImpl(); } }
TestBean3.java
package com.stonegeek; import com.stonegeek.service.BeanFactory; import com.stonegeek.service.config.BeanConfig; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * Created by StoneGeek on 2018/5/13. */ public class TestBean3 { @Test public void test(){ ApplicationContext applicationContext=new AnnotationConfigApplicationContext(BeanConfig.class); BeanFactory beanFactorys=applicationContext.getBean(BeanFactory.class); beanFactorys.Beantest(); //This is a 基於類的Java Config Bean! } }
以上就是spring bean的三種配置方式的簡單介紹!!