spring系列 注解开发

本文详细介绍了Spring框架中的注解开发,包括@Component及其派生注解@Controller、@Service、@Repository,@Configuration用于创建配置类,@ComponentScan进行包扫描,@Scope设置bean的作用域,以及@PostConstruct和@PreDestroy管理bean生命周期。此外,还讨论了@Autowired和@Qualifier的自动装配,@Value的值注入,@PropertySource读取配置文件,@Import引入配置类,@Bean创建bean的方法,最后展示了注解的综合使用案例。
摘要由CSDN通过智能技术生成

@Component

bean的类上加注解@Component,value是否设置值,取决于用id获取还是根据class名获取,id获取就要给一个value对应id值。

xml中要先指定命名空间,在接component-scan开启包扫描,后面接要扫描那个路径下的包:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.xxz"/>

</beans>

Spring提供@Component注解的三个衍生注解

@Controller:用于表现层bean定义

@Service:用于业务层bean定义

@Repository:用于数据层bean定义

@Configuration

用于设定当前类为配置类

作用是:spring对xml结构的简化,xml结构就是下面这块:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

</beans>

@ComponentScan

用于设定扫描路径,此注解只能添加一次,多个数据用数组格式

也就是简化xml中的

<context:component-scan base-package="com.xxz"/>

bean模式:@Scope("singleton")

bean生命周期:@PostConstruct、@PreDestroy

public class BookDaoImpl implements BookDao {    

        @PostConstruct    

        public void init(){        

                System.out.println("book init ...");    

        }    

        @PreDestroy    

        public void destroy(){        

                System.out.println("book destory ...");    

        }

}

自动装配:@Autowired、@Qualifier

自动装配默认使用无参构造方法创建对象,并基于反射创建对象,因此无需提供set方法(之前的byType模式需要set方法,是因为有可能方法定义了private,框架考虑的是不能越俎代庖,现在为了方便都可以反射了)。

@Autowired是基于 byType模式的,需要bean唯一,当有多个相同类型的bean需要初始化时会报错,为了解决这个问题可以采用下面的@Qualifier注解,bean的类上里面配上bean的id,@Repository("bookDao"),@Qualifier配上对应的id

@Qualifier不能单独使用,要依赖于@Autowired,没有的话没有注入调用会报空指针。

值注入:@Value

读取配置文件:@PropertySource

多个路径书写为字符串数组格式,这里不支持使用通配符,会将*视为文件名的一部分,lasspath:可加可不加。

引入起来配置类:@Import

配置信息可能会有很多,所以我们用一个配置类专门管理,用@Import引入其他配置类。

注入返回方法对象:@Bean

表示当前方法的返回值是一个bean。

@Bean修饰的方法,形参根据类型自动装配

注解的综合用法:

定义bean:

@Repository

//@Component("bookDao")

@Scope("singleton")

public class BookDaoImpl implements BookDao {    

        @Value("${name}")   

        private String connectionNum;

        @PostConstruct    

        public void init(){        

                System.out.println("book init ...");    

        }    

        @PreDestroy    

        public void destroy(){        

                System.out.println("book destory ...");    

        }

}

注入:

@Service

public class BookServiceImpl implements BookService {    

        @Autowired   

        //@Qualifier("bookDao1") 

        private BookDao bookDao;  

        public void setBookDao(BookDao bookDao) {        

                this.bookDao = bookDao;    

        }    

        public void save() {        

                System.out.println("book service save ...");        

                bookDao.save();    

        }

}

@Configuration声明当前类为Spring配置类,

@Configuration
//设置bean扫描路径,多个路径书写为字符串数组格式
@ComponentScan({"com.xxz.service","com.xxz.dao"})

//jdbc.properties放在的resources目录下

@PropertySource("classpath:jdbc.properties")

@Import({JdbcConfig.class})
public class SpringConfig {
}

public class JdbcConfig {
    //1.定义一个方法获得要管理的对象
    @Value("com.mysql.jdbc.Driver")
    private String driver;
    @Value("jdbc:mysql://localhost:3306/spring_db")
    private String url;
    @Value("root")
    private String userName;
    @Value("root")
    private String password;

    //当有依赖对象注入需求时,由于总配置类开启了包扫描,所以这里只需要给BookDao bookDao,框架就帮我们自动注入了。
    @Bean
    public DataSource dataSource(BookDao bookDao){
        System.out.println(bookDao);
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(userName);
        ds.setPassword(password);
        return ds;
    }
}

jdbc.properties:

name=888

启动类中采用注解容器:

ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);

DataSource dataSource = ctx.getBean(DataSource.class);
System.out.println(dataSource);

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

呀吼呀吼

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

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

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

打赏作者

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

抵扣说明:

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

余额充值