在spring的学习中,必定会遇到两个标签的使用,一个是:
<context:annotation-config/>
一个是:
<context:component-scan base-package="cn.com.lzxh"/>
那么这两个标签有什么作用,很多初学者会感到困惑,今天在这里做一个详细的分析,
<context:component-scan base-package="cn.com.lzxh"/>标签的作用是自动扫描,它会将base-package指定位置下的具有spring对应注解(@Component,@Service,@Repository,@Controller等)的类在加载容器时都注入springIOC容器,不用再单独使用bean标签注册,当我们使用对应类时,只需要使用@Autowired注解即可自动注入对象,该标签包含了<context:annotation-config/>标签的功能。因此使用<context:component-scan base-package="cn.com.lzxh"/>标签时,可以将<context:annotation-config/>标签省去,使用案例如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="myDataSource"/>
</bean>
<!-- <bean id="bookDao" class="cn.com.lzxh.dao.impl.BookDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="bookService" class="cn.com.lzxh.service.impl.BookServiceImpl">
<property name="bookDao" ref="bookDao"></property>
</bean> -->
<!-- 自动扫描:base-package代表spring要扫描的基本包,
@Component: 组件
@Service:业务层组件
@Repository: 持久层组件
@Controller:表现层组件
该包下的具有spring对应注解的类在spring的IOC容器加载时,都会被注册到IOC容器中 -->
<context:component-scan base-package="cn.com.lzxh"/>
</beans>
实体类:
package cn.com.lzxh.pojo;
import org.springframework.stereotype.Component;
@Component
public class Book {
private String b_code;
private String b_name;
private String b_author;
private String b_publish;
private String b_pubdate;
private double b_price;
public String getB_code() {
return b_code;
}
public void setB_code(String b_code) {
this.b_code = b_code;
}
public String getB_name() {
return b_name;
}
public void setB_name(String b_name) {
this.b_name = b_name;
}
public String getB_author() {
return b_author;
}
public void setB_author(String b_author) {
this.b_author = b_author;
}
public String getB_publish() {
return b_publish;
}
public void setB_publish(String b_publish) {
this.b_publish = b_publish;
}
public String getB_pubdate() {
return b_pubdate;
}
public void setB_pubdate(String b_pubdate) {
this.b_pubdate = b_pubdate;
}
public double getB_price() {
return b_price;
}
public void setB_price(double b_price) {
this.b_price = b_price;
}
@Override
public String toString() {
return "Book [b_code=" + b_code + ", b_name=" + b_name + ", b_author=" + b_author + ", b_publish=" + b_publish
+ ", b_pubdate=" + b_pubdate + ", b_price=" + b_price + "]";
}
}
持久层实现类:
package cn.com.lzxh.dao.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import cn.com.lzxh.dao.BookDao;
import cn.com.lzxh.pojo.Book;
//持久层
@Repository("bookDao")
public class BookDaoImpl implements BookDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<Book> findAll() {
List<Book> list = jdbcTemplate.query("select * from book", new BeanPropertyRowMapper(Book.class));
return list;
}
}
业务层实现类:
package cn.com.lzxh.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import cn.com.lzxh.dao.BookDao;
import cn.com.lzxh.pojo.Book;
import cn.com.lzxh.service.BookService;
//业务层
@Service("bookService")
public class BookServiceImpl implements BookService {
//自动注入
@Autowired
@Qualifier(value="bookDao")
//@Resource(name="bookDao2")
private BookDao bookDao;
@Override
public void findAll() {
List<Book> list = bookDao.findAll();
for(Book b : list) {
System.out.println(b);
}
}
}
测试类:
package cn.com.lzxh.test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.com.lzxh.service.BookService;
public class BookTest {
public static void main(String[] args) {
ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("spring-beans.xml");
BookService bs = ac.getBean("bookService", BookService.class);
bs.findAll();
ac.close();
}
}
运行结果:
<context:annotation-config/>标签的作用是可以自动声明@Autowired、@Resource 、@PostConstruct、@PreDestroy等注解,这些注解很常用,如果按照传统bean标签的配置方式将会变得很繁琐,因此可以使用该标签代替,但是bean标签注册bean对象的过程不可省略。如下所示,案例中只有配置文件有区别:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="myDataSource"/>
</bean>
<!-- 使用该标签时,单独注册bean即可,不需要再进行property依赖注入 -->
<context:annotation-config/>
<bean id="bookDao" class="cn.com.lzxh.dao.impl.BookDaoImpl"/>
<bean id="bookService" class="cn.com.lzxh.service.impl.BookServiceImpl"/>
<!-- <bean id="bookDao" class="cn.com.lzxh.dao.impl.BookDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="bookService" class="cn.com.lzxh.service.impl.BookServiceImpl">
<property name="bookDao" ref="bookDao"></property>
</bean> -->
<!-- 自动扫描:base-package代表spring要扫描的基本包,
@Component: 组件
@Service:业务层组件
@Repository: 持久层组件
@Controller:表现层组件
该包下的具有spring对应注解的类在项目启动时,都会被注册到IOC容器中 -->
<!-- <context:component-scan base-package="cn.com.lzxh"/> -->
</beans>
运行结果与上个案例相同: