目录
一、传统的XML配置方式
二、基于java注解的配置
三、基于类的Java Config
正文
Spring Bean有三种配置方式:
传统的XML配置方式基于注解的配置基于类的Java Config
添加spring的maven repository
一、传统的XML配置方式
BeanFactory.java
package com.stonegeek.service;
public interface BeanFactory {
public void Beantest();
}
BeanFactoryImpl.java
package com.stonegeek.service.impl;
import com.stonegeek.service.BeanFactory;
public class BeanFactroyImpl implements BeanFactory {
@Override
public void Beantest() {
System.out.println("----------------This is a 传统的XML配置的bean!-------------------");
}
}
applicationContext.xml
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 ">
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;
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注解的配置
使用组件扫描可以不必在Spring的配置文件中配置各个`
`base-package`表示需要扫描的“根包”,当配置后,Spring容器会自动扫描根包下所有类,及各及子包类。
目的:让Spring知道有哪些类,事实上,只是配置组件扫描,Spring并不会创建这些类的对象。如需要Spring创建某些类的对象,还需要为这些类添加注解!
常用类注解: @Component---------通用注解(推荐) @Named-------------通用注解(不推荐) @Service-----------业务逻辑类注解 @Controller--------控制器类注解 @Repository--------持久层(数据访问层)处理类注解 >以上5种注解是等效的!只是语义不同!(推荐采用对应的)
当同时配置组件扫描和注解后,Spring就会创建这些对象!
Spring创建类的对象时,默认使用bean的id是将类的首字母改为小写:
class:Userao---->bean id:userDao
如果需要自定义名称,可以在注解上添加配置:
@Component("dao")
BeanFactoryImpl.java
package com.stonegeek.service.impl;
import com.stonegeek.service.BeanFactory;
import org.springframework.stereotype.Service;
@Service("beanFactory")
public class BeanFactroyImpl implements BeanFactory {
@Override
public void Beantest() {
System.out.println("----------------This is a 基于Java注解的bean!-------------------");
}
}
applicationContext.xml
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 ">
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;
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;
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;
@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;
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的三种配置方式的简单介绍!!