spring_注入方式笔记

2018-10-13  本文已影响0人  回去生气0101

spring容器负责生成实例

package com.bless.springdemo.action; 
public class SpringAction { 
//注入对象springDao 
private SpringDao springDao; 
//一定要写被注入对象的set方法 
public void setSpringDao(SpringDao springDao) { 
this.springDao = springDao; 
}
  1. 随后编写spring的xml文件,
  2. <bean>中的name属性是class属性的一个别名,class属性指类的全名,因为在SpringAction中有一个公共属性Springdao,所以要在<bean>标签中创建一个<property>标签指定SpringDao。
  3. <property>标签中的name就是SpringAction类中的SpringDao属性名,ref指下面<bean name="springDao"...>,这样其实是spring将SpringDaoImpl对象实例化并且调用SpringAction的setSpringDao方法将SpringDao注入.
public class SpringAction { 
//注入对象springDao 
private SpringDao springDao; 
private User user;
public SpringAction(SpringDao springDao,User user){  
    this.springDao = springDao;  
    this.user = user;  
    System.out.println("构造方法调用springDao和user");  
}  

    public void save(){  
    user.setName("卡卡");  
    springDao.save(user);  
} 

  1. 在XML文件中同样不用<property>的形式,而是使用<constructor-arg>标签,ref属性同样指向其它<bean>标签的name属性
package com.cjwdemo.demoBySpring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.cjwdemo.demoBySpring.bean.Demo1;
import com.cjwdemo.demoBySpring.bean.Demo2;

@Configuration // @Configuration表示当前类为配置类
/*
 * @ComponentScan默认扫描当前包和子包的class文件
 * 有@Controller或@Component或@Service或@Repository则自动注册到spring管理
 */
@ComponentScan
public class Application {


    // 通过@Configuration和@Bean实现bean注册
    @Bean
    public Demo1 setDemo1() {
        return new Demo1();
    }

    public static void main(String[] args) {
        /*
         * ApplicationContext是BeanFactory的超类,需要通过配置类或xml配置文件创建出来,
         * 能通过该类直接获取spring管理的bean
         */
        ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
        Demo1 demo1 = context.getBean(Demo1.class);
        demo1.test();
        Demo2 demo2 = context.getBean(Demo2.class);
        demo2.test();
    }
}
// 获取demo1 bean成功
// 获取demo1 bean成功

package com.cjwdemo.demoBySpring.bean;

public class Demo1 {

    public void test() {
        System.out.println("获取demo1 bean成功");
    }

}
package com.cjwdemo.demoBySpring.bean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Demo2 {

    @Autowired
    private Demo1 demo1;

    public void test() {
        demo1.test();
    }

}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cjwdemo.demoBySpring</groupId>
    <artifactId>demoBySpring</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>demoBySpring Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.8.RELEASE</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>demoBySpring</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

上一篇下一篇

猜你喜欢

热点阅读