spring setter注入

时间:2019-11-25
本文章向大家介绍spring setter注入,主要包括spring setter注入使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Bean.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"   
    xmlns:aop="http://www.springframework.org/schema/aop"       
    xmlns:tx="http://www.springframework.org/schema/tx"         
    xmlns:util="http://www.springframework.org/schema/util"         
    xmlns:p="http://www.springframework.org/schema/p"               
    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    
                        http://www.springframework.org/schema/aop                               
                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                        http://www.springframework.org/schema/tx                            
                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd                      
                        http://www.springframework.org/schema/util                          
                        http://www.springframework.org/schema/util/spring-util-3.0.xsd"
                        default-init-method="defaultInit" default-destroy-method="defaulfDestroy">  
     
   <bean id="all_OP" class="service.All_OP">
           <!--  <property name="all_atomic" ref="all_atomic"></property> -->
           <constructor-arg ref="all_Atomic"></constructor-arg>
   </bean> 
      
   <bean id="all_Atomic" class="service.All_Atomic">
   </bean> 
    
    <!-- <context:component-scan base-package="service,test" /> -->
</beans>

All_Atomic.java  随便

All_OP.java

public class All_OP extends TestBase{
    private All_Atomic all_atomic;
    public All_OP(All_Atomic all_atomic) {         
        // TODO Auto-generated constructor stub
        this.all_atomic=all_atomic;
    }

TestBase.java

public class TestBase  {
    private ClassPathXmlApplicationContext context;
    private String springXmlPath;
    
    public TestBase() {
        springXmlPath="classpath*:Beans.xml";
    }
  @BeforeClass
  public void beforeClass() {
      context=new ClassPathXmlApplicationContext(getSpringXmlPath().split("[,\\s]+"));
      //context.start();
  }

  @SuppressWarnings("unchecked")
public <T extends Object> T getBean(String beanId) {
    return (T)context.getBean(beanId);
  } 
  

public <T extends Object> T getBean(Class<T> clazz) {
    return (T)context.getBean(clazz);
}

public void setSpringXmlPath(String springXmlPath) {
    this.springXmlPath = springXmlPath;
}
public String getSpringXmlPath() {
    return springXmlPath;
}


}

原文地址:https://www.cnblogs.com/saifei1125/p/11927608.html