spring setter注入

Posted saifei1125

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}


}

 

以上是关于spring setter注入的主要内容,如果未能解决你的问题,请参考以下文章

[Spring实战系列]Spring注入方式之setter注入

java的Spring学习1--spring引用及属性(setter,getter)注入

Spring依赖注入的Setter注入(通过get和set方法注入)

Spring5依赖注入常用的三种方法:构造注入Setter注入自动装配

Spring使用Setter依赖注入

spring之setter注入