四.简化Spring XML的配置

一.自动装配
byName:

<bean  id="customer" class="com.jike.***.Customer” autowire="byName" /> 
<bean  id="person" class="com.jike.***.Person" />

byType:

<bean id="customer" class="com.jike.***.Customer” autowire="byType" /> 
<bean id="person" class="com.jike.***.Person"  />

<bean id="customer" class="com.jike.***.Customer” autowire="byType" /> 
<bean id="person1" class="com.jike.***.Person"  primary=“false” />

<bean id="customer" class="com.jike.***.Customer” autowire="byType" /> 
<bean id="person2" class="com.jike.***.Person"  autowire-candidate=“false” />

constructor:

<bean id="customer" class="com.jike.***.Customer” autowire=“constructor" /> 
<bean id="person" class="com.jike.***.Person"  />

autodetect:

<bean id="customer" class="com.jike.***.Customer” autowire=“autodetect" /> 
<bean id="person" class="com.jike.***.Person"  />

可以在属性中加default-autowire=“byType”开启

此外自动和手动和混合:

<bean id="customer" class="com.jike.***.Customer“autowire=“byType" >
      <property name="person" ref="person1" />
</bean>
 <bean id="person"      class="com.jike.***.Person" />
 <bean id="person1"   class="com.jike.***.Person" />

<bean id="customer" class="com.jike.***.Customer“autowire=“byType" >
      <property name="person" ><null/></property>
</bean>
 <bean id="person"     class="com.jike.***.Person" />
 <bean id="person1"  class="com.jike.***.Person" />

二.基于注解的配置.
定义Bean的注解:
@Component
@Controller
@Repository
@Service

标注后扫描类包加载定义的Bean

 xmlns:context="http://www.springframework.org/schema/context"    ① 
  <context:component-scan   base-package="com.jike.spring"/>   ② 

生命周期注解:
以前:

<bean  id=“userService”      class=“com.jike.***.UserService”  init-      
method=“initdestroy-method=“destroy”> 
</bean> 

现在:

public class PersonService {  
    @PostConstruct  
    public void  init(){  ……}  
    @PreDestroy  
    public void  dostory(){  …… }  
} 
<context:annotation-config />

常用注解详解:

@Servicepublic class LogonService {
    @Autowiredprivate LogDao logDao;
}

public class LogonService {
    @Autowired(required=false)③
    private LogDao logDao;
}

public class LogonService {
    @Autowired
    @Qualifier(“userDao”)④
    private UserDao userDao;
}

@Autowired标注方法入参示例:

public class LogonService {
    @Autowiredpublic void setLogDao(LogDao logDao) {
              this.logDao = logDao;
      }
     @Autowired
     @Qualifier(“userDao”) ②
     public void setUserDao(UserDao userDao) {
              this.userDao = userDao;
      }
}

@Autowired
public void init(@Qualifier(“userDao”)UserDao userDao) {③
    this.userDao = userDao;
}

@Autowired标注集合入参示例:

public class LogonService {
    @Autowired(required=false) ①
    public List<Plugin> plugins ;
    public List<Plugin> getPlugins() {
        return plugins;
    }
}

三.基于JAVA类的配置:目的消灭XML
JAVA类定义Bean配置元数据步骤:
@Configuration注解需要作为配置的类
@Bean注解相应的方法
AnnotationConfigApplicationContext或子类进行加载

@Configuration   
public class ApplicationContextConfig {  
    @Bean  
    public String message() {  
        return "hello";  
    }  
} 

加载:
public class ConfigurationTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx =
        new AnnotationConfigApplicationContext(ApplicationContextConfig.class);
        System.out.println(ctx.getBean("message"));
    }
}

@Bean

@Bean
public String message() {
    return new String("hello");
}
<bean id="message" class="java.lang.String">  
    <constructor-arg index="0" value="hello"/>  
</bean> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值