Spring(1)-依赖注入

13 篇文章 0 订阅

1.1依赖注入

1.1.1 说明

我们经常说的控制翻转( Inversion of Control-IOC)和依赖注入(dependency injection-DI)在Spring环境下是等同的概念,控制翻转是通过依赖注入实现的。所谓依赖注入指的是容器负贵创建对象和维护对象间的依赖关系,而不是通过对象本身负责自己的创建和解决自己的依赖。
Spring loC容器(ApplicationContext) 负贵创建Bean,并通过容器将功能类Bean注入到你需要的Bean中。Spring 提供使用xml、注解、Java配置、groovy配置实现Bean的创建和注入。

声明Bean的注解:
@Component组件,没有明确的角色。
@Service在业务逻辑层( service层)使用。
@Repository在数据访问层(dao 层)使用。
@Controller 在展现层( MVC- +Spring MVC)使用。

注入Bean的注解,- - 般情况下通用。
@Autowired: Spring 提供的注解。
@Inject: JSR-330 提供的注解。
@Resource: JSR-250 提供的注解。

1.1.2.代码演示

引入Spring相关配置

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lglg</groupId>
    <artifactId>spring-demo01</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
1.1.2.1 第一种 Java配置

编写简单的java对象

package com.lglg.springdemo01;

import org.springframework.stereotype.Service;

/**
 * Date:2020/8/9
 *
 * @author:lg
 */
//@Service
public class FunctionService {

    public String sayHello(String word){

        return "Hello" + word + "!";
    }
}

package com.lglg.springdemo01;

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

/**
 * Date:2020/8/9
 *
 * @author:lg
 */
//@Service
public class UserFunctionService {

    @Autowired // 在本类中通过Spring容器中的bean注入到该类中
    FunctionService functionService;

    public String sayHello(String word){

        return functionService.sayHello(word);
    }

}

编写配置文件

package com.lglg.config;

import com.lglg.springdemo01.FunctionService;
import com.lglg.springdemo01.UserFunctionService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Date:2020/8/9
 *
 * @author:lg
 */
@Configuration
@ComponentScan("com.lglg")// 扫面相应的包路径
public class LgConfig {

    @Bean// 通过该注解将该方法上的bean注入到Spring容器中
    public FunctionService functionService(){
        return new FunctionService();
    }

    @Bean
    public UserFunctionService userFunctionService(){
        return new UserFunctionService();
    }
}

测试Demo01

package com.lglg.demoTest;

import com.lglg.config.LgConfig;
import com.lglg.springdemo01.UserFunctionService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Date:2020/8/9
 *
 * @author:lg
 */
public class DemoTest01 {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(LgConfig.class);
        UserFunctionService bean = annotationConfigApplicationContext.getBean(UserFunctionService.class);

        System.out.println(bean.sayHello("lg"));
        annotationConfigApplicationContext.close();
    }
}

控制台输出
在这里插入图片描述

第二种
package com.lglg.springdemo01;

import org.springframework.stereotype.Service;

/**
 * Date:2020/8/9
 *
 * @author:lg
 */
@Service
public class FunctionService {

    public String sayHello(String word){

        return "Hello" + word + "!";
    }
}

package com.lglg.springdemo01;

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

/**
 * Date:2020/8/9
 *
 * @author:lg
 */
@Service
public class UserFunctionService {

    @Autowired // 在本类中通过Spring容器中的bean注入到该类中
    FunctionService functionService;

    public String sayHello(String word){

        return functionService.sayHello(word);
    }

}

配置类(直接使用注解将上面的类交给Spring容器管理,无需在通过手动注入)

package com.lglg.config;

import com.lglg.springdemo01.FunctionService;
import com.lglg.springdemo01.UserFunctionService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Date:2020/8/9
 *
 * @author:lg
 */
@Configuration
@ComponentScan("com.lglg")// 扫面相应的包路径
public class LgConfig {

//    @Bean// 通过该注解将该方法上的bean注入到Spring容器中
//    public FunctionService functionService(){
//        return new FunctionService();
//    }
//
//    @Bean
//    public UserFunctionService userFunctionService(){
//        return new UserFunctionService();
//    }
}
`

Test

package com.lglg.demoTest;

import com.lglg.config.LgConfig;
import com.lglg.springdemo01.UserFunctionService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Date:2020/8/9
 *
 * @author:lg
 */
public class DemoTest01 {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(LgConfig.class);
        UserFunctionService bean = annotationConfigApplicationContext.getBean(UserFunctionService.class);

        System.out.println(bean.sayHello("lg"));
        annotationConfigApplicationContext.close();
    }
}

输出结果一样

总结:

本次代码解释
①使用AnnotationConfigApplicationContext作为Spring容器,接受输入一个配置类作为
参数;
②获得声明配置的UseFunctionService的Bean。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值