Spring 注解开发下的依赖注入(自动装配)(引用类型)(普通类型)(加载properties文件)

必读: 

1.Spring注解开发中的东西是为了让我门加速开发的,他对原始的内容进行了阉割,也就是没有必要的功能他就没有做,只做了最快速,最好用的,也就是用自动装配替代了Setter与构造器注入

注入引用类型

1.使用@Autowired注解开启自动装配模式(按类型)

注意:自动装配给予反射设计创建对象并暴力反射对应属性为私有属性初始化数据,因此无需提供Setter方法

注意:自动装配简易使用无参构造方法创建对象(默认),如果不提供对应构造方法,晴提供唯一的构造方法

2.使用@Qualifier注解开启指定名称装配bean

注意:@Qualifier注解无法单独使用,必须配合@Autowired注解使用

注入普通类型

1.使用@Value()注解开注入普通类型

@Value("${name}")   (注入properties文件的格式)

@Value("20")             (注入普通类型格式)

加载properties文件

使用@PropertySource({"jdbc.properties","jdbc2.properties"})

注解加载properties配置文件,

注意:路径仅支持单一文件配置,多文件使用数组格式进行配置,不允许使用通配符

注入引用类型(案例)

BookDaoImpl (dao层实现类1)

package com.itheima.dao.impl;

import com.itheima.dao.BookDao;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

@Repository("bookDao")
public class BookDaoImpl implements BookDao {

    public void save() {
        System.out.println("bookDao1 save ...");
    }

}

BookDaoImpl2 (dao层实现类2)

package com.itheima.dao.impl;

import com.itheima.dao.BookDao;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

@Repository("bookDao2")
public class BookDaoImpl2 implements BookDao {

    public void save() {
        System.out.println("bookDao2 save ...");
    }
}

BookServiceImpl(Service层实现类)

package com.itheima.service.impl;

import com.itheima.dao.BookDao;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class BookServiceImpl implements BookService {

    @Autowired                //自动装配,暴力装值,不需要Setter方法,自动注入,但是必须是唯一,不唯一的化,则就按名称
    @Qualifier("bookDao2")    //指定加载的bean类型(如果有很多相同类型的bean,则需要指定加载的bean名称,此注解依赖于@Autowired)
    private BookDao bookDao;

    @Override
    public void save() {
        System.out.println("Service save...");
        bookDao.save();
    }
}

app(实现类)

package com.itheima;

import com.itheima.config.SpringConfig;
import com.itheima.service.BookService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App2 {
    public static void main(String[] args) {

        //1.加载配置类
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);

        //2.获取bean
        BookService service = ctx.getBean(BookService.class);

        //输出bean
        service.save();

    }
}

效果1(指定装配相同引用类型BookDaoImpl ):

 效果2(指定装配相同引用类型BookDaoImpl2):

注入普通类型(案例)

BookDaoImpl (dao层实现类)

package com.itheima.dao.impl;

import com.itheima.dao.BookDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

@Repository("bookDao")
public class BookDaoImpl implements BookDao {

    @Value("${name}")
    private String name;

    @Value("20")
    private Integer age;


    public void save() {
        System.out.println("bookDao1 save ..."+name+"+"+age+"");
    }

}

BookServiceImpl(Service层实现类)

package com.itheima.service.impl;

import com.itheima.dao.BookDao;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class BookServiceImpl implements BookService {

    @Autowired                //自动装配,暴力装值,不需要Setter方法,自动注入,但是必须是唯一,不唯一的化,则就按名称
    @Qualifier("bookDao")    //指定加载的bean类型(如果有很多相同类型的bean,则需要指定加载的bean名称,此注解依赖于@Autowired)
    private BookDao bookDao;

    @Override
    public void save() {
        System.out.println("Service save...");
        bookDao.save();
    }
}

app(实现类)

package com.itheima;

import com.itheima.config.SpringConfig;
import com.itheima.service.BookService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App2 {
    public static void main(String[] args) {

        //1.加载配置类
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);

        //2.获取bean
        BookService service = ctx.getBean(BookService.class);

        //输出bean
        service.save();

    }
}

SpringConfig (Spring Java 类配置文件,加载properties文件)

package com.itheima.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;


@Configuration                       //代表了这是个配置类,以此与applicationContext.xml基础配置文件相当
@ComponentScan({"com.itheima"})      //包扫描  底层为数组,支持通配符
@PropertySource({"jdbc.properties","jdbc2.properties"})   //加载properties文件,配置文件多,底层为数组,不支持通配符
public class SpringConfig {

}

properties文件

效果(内容含义普通类型的value注入问题,及如何把加载的properties文件中的值当道value之中):

 小结:

自动装配

    1.引用类型

        @Autowired

        @Qualifier

    2.普通类型

        @Value

读取properties文件

    @PropertySource

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

很丧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值