Bean配置

1、xml配置(摘抄自:https://www.cnblogs.com/zyx1301691180/p/7665971.html)

  一、setter方法配置Bean:

    1、创建一个 Spring Bean Configuration File

    2、配置Bean代码:

<bean id="address" class="com.spring.xmlBean.Address"></bean>
<bean id="person" class="com.spring.xmlBean.Person"></bean>

    3、给Bean配置属性:

    <!-- 添加 Address 和 Person -->
    <bean id="address" class="com.spring.xmlBean.Address">
        <property name="city" value="日照"></property>
        <property name="province" value="山东"></property>
    </bean>
    
    <bean id="person" class="com.spring.xmlBean.Person">
        <property name="name" value="Little-Koala"></property>
        <property name="age" value="18"></property>
        <!-- 通过 ref 引用了 address 这个 bean -->
        <property name="address" ref="address"></property>
    </bean>

  二:构造器配置Bean

<bean id="address" class="com.spring.xmlBean.Address">
        <constructor-arg name="city" value="日照"></constructor-arg>
        <constructor-arg name="province" value="山东"></constructor-arg>
        <!-- 
        <property name="city" value="日照"></property>
        <property name="province" value="山东"></property>
         -->
</bean>

2、基于注解的配置

  一:注解解析

  @Component 是通用标注

  @Controller 标注 web 控制器

  @Service 标注 Servicec 层的服务

  @Respository 标注 DAO 层的数据访问

  二:注解配置Bean

  使用注解配置bean时候,并没有指定bean的id,那么Spring帮我们创建 bean时候会给一个默认的id,id为类名首字母小写

  当类的名字是以两个或以上的大写字母开头的话,bean的名字会与类名保持一致

@Service("beanFactory") 
public class BeanFactroyImpl implements BeanFactory {

  @Autowired   private UserRepository userRepository;
}
@Component
public class LogonService {}

  三:配置文件

  需要在applicationContext.xml文件中加一行,作用是自动扫描base-package包下的注解:

<context:component-scan base-package="com.stonegeek" />

3、java代码配置

  通过java类定义spring配置元数据,且直接消除xml配置文件

  Spring3.0基于java的配置直接支持下面的注解:

  @Configuration、@Bean、@DependsOn、@Primary、@Lazy、@Import、@ImportResource、@Value

  使用java配置的方式,那么就不需要在类上写注解了,直接在配置类里面进行申明即可:

@Configuration // 通过该注解来表明该类是一个Spring的配置,相当于一个xml文件
public class CDPlayConfig { @Bean // 通过该注解来表明是一个Bean对象,相当于xml中的<bean>,默认情况下,bean的ID与带有@bean注解的方法名一样 public CompactDisc sgtPeppers(){ return new SgtPeppers();// 直接new对象做演示 }
  @Bean
    public CDPlayer cdPlayer() {//由于@Bean注解的存在,却是阻止了方法的调用,直接返回该方法创建的实例对象SgtPapers
        return new CDPlayer(sgtPeppers()); }

  @Bean
  public CDPlayer cdPlayer(CompactDisc compactDisc){ //传入一个CompactDisc对象,通过构造器完成注入
    return new CDPlayer(compactDisc);
  } 

  @Bean   public CDPlayer cdPlayer(CompactDisc compactDisc){ //setter属性的注入   CDPlayer cdPlayer=new CDPlayer(compactDisc);   cdPlayer.setCompactDisc(compactDisc);   return cdPlayer;   }
}

4、混合配置

  @Import注解,将两个配置类组合在一起,组成一个新的配置类

@Configuration
@Import({CDConfig.class,CDPlayerConfig.class})
public class SoundSystemConfig { }

   @ImportSource注解,将配置文件和XML共同的加载到Spring文件中

  @Configuration
  @ComponentScan
  @Import(CDConfig.class)
  @ImportResource("classpath:applicationContext.xml")
  public class CDPlayerConfig { 
    @Bean
    public CDPlayer compactDisc(CompactDisc compactDisc){
      return new CDPlayer(compactDisc);
    }
  }

  XML 中引入JavaConfig

  

猜你喜欢

转载自www.cnblogs.com/yanghanwen/p/12080930.html