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的三种配置方式的简单介绍!!