1. 注解开发定义Bean对象
目的:xml配置Bean对象有些繁琐,使用注解简化Bean对象的定义
1.1 在applicationContext.xml中开启Spring注解包扫描
<?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">
<!--扫描com.lin包及其子包下的类中注解-->
<context:component-scan base-package="com.lin"/>
</beans>
1.2 在类上使用@Component注解定义Bean。
BookDaoImpl
package com.lin.dao.daoimpl;
import com.lin.dao.BookDao;
import org.springframework.stereotype.Component;
@Component("bookDao")
public class BookDaoImpl implements BookDao {
public void save() {
System.out.println("数据层的save");
}
}
BookServiceImpl
package com.lin.service.serviceimpl;
import com.lin.dao.BookDao;
import com.lin.service.BookService;
import org.springframework.stereotype.Component;
@Component
public class BookServiceImpl implements BookService {
private BookDao bookDao;
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
public void save() {
bookDao.save();
System.out.println("业务层的save");
}
}
补充说明:如果@Component注解没有使用参数指定Bean的名称,那么类名首字母小写就是Bean在IOC容器中的默认名称。例如:BookServiceImpl对象在IOC容器中的名称是bookServiceImpl。
1.3 在测试类中获取Bean对象
package com.lin.app;
import com.lin.dao.BookDao;
import com.lin.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application {
public static void main(String[] args) {
//1.获取spring容器,加载配置文件
ApplicationContext springContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//按id并指定类型获取bean
BookDao bookDao = springContext.getBean("bookDao", BookDao.class);
bookDao.save();
//按类型获取bean
BookService bookService = springContext.getBean(BookService.class);
System.out.println(bookService);
}
}
在测试类中不要调用bookService的save方法,因为还没有给BookServiceImpl中的bookDao赋值,调用bookService的save方法会出现空指针异常。
1.4 运行结果
2. 纯注解开发模式
2.1 纯注解开发模式介绍
- Spring3.0开启了纯注解开发模式,使用Java类替代配置文件,开启了Spring快速开发赛道
- Java类代替Spring核心配置文件
2.2 代码演示
2.2.1 定义配置类代替配置文件
package com.lin.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//声明当前类为Spring配置类
@Configuration
//Spring注解扫描,相当于<context:component-scan base-package="com.lin"/>
@ComponentScan("com.lin")
public class SpringConfig {
}
2.2.2 在测试类中加载配置类,获取Bean对象并使用
public class AppForAnnotation {
public static void main(String[] args) {
//AnnotationConfigApplicationContext加载Spring配置类初始化Spring容器
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
System.out.println(bookDao);
//按类型获取bean
BookService bookService = ctx.getBean(BookService.class);
System.out.println(bookService);
}
}