<beans xmlns="http://www.springframework.org/schema/beans"
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
">
<!-- BookDao Bean -->
<bean id="bookDao" class="com.study.DaoImpl.BookDaoImpl"></bean>
<!-- BookService Bean-->
<bean id="bookService" class="com.study.Service.BookService">
<!-- BookService中的声明了BookDao对象,通过ref属性将BookDao的bean注入到对象中 -->
<property name="bookDao" ref="bookDao"/>
</bean>
</beans>
public class BookService {
private BookDao bookDao;
public BookService() {
System.out.println("BookService实例化");
}
public void setBookDao(BookDao bookDao) {
System.out.println("BookService属性初始化装配成功");
this.bookDao = bookDao;
}
public void storeBook(String bookname){
System.out.println("图书上架");
System.out.println(bookDao.addBook(bookname));
}
}
public class TestDmeo {
BookService bookService;
@Test
public void testStoreBook(){
System.out.println("容器初始化");
ApplicationContext app = new ClassPathXmlApplicationContext("bean.xml");
bookService = (BookService) app.getBean("bookService");//将对象注入到声明好的BookService对象中。(bookService就是配置文件中的id)
bookService.storeBook("Spring MVC");
}
}
public interface BookDao {
public String addBook(String BookName);
}
public class BookDaoImpl implements BookDao {
public BookDaoImpl() {
System.out.println("BookDao实例化");
}
public String addBook(String BookName) {
return BookName+"添加成功";
}
}
<context:component-scan base-package="com.study" resource-pattern="Service/B*.class"/>
| [b]Filter Type[/b] | [b]Examples Expression[/b] | [b]Description[/b] |
| annotation | org.example.SomeAnnotation | 符合SomeAnnoation的target class(注解类) |
| assignable | org.example.SomeClass | 指定class或interface的全名(指定确切的类或接口) |
| aspectj | org.example..*Service+ | AspectJ语法 |
| regex | org\.example\.Default.* | Regelar Expression (正则表达式) |
| custom | org.example.MyTypeFilter | Spring3新增自定义Type,org.springframework.core.type.TypeFilter |
<!-- 容器扫描包下的注解配置组件 -->
<context:component-scan base-package="com.study" use-default-filters="false">
<context:include-filter type="aspectj" expression="com.study.Service.*"/> <!-- 模糊过滤 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/><!-- 过滤指定的注解 -->
<context:include-filter type="assignable" expression="com.study.Service.BookService"/><!-- 过滤指定的类或接口,路径要完整,如果是接口的话,所有派生类都会被过滤 -->
<context:include-filter type="regex" expression="com.*"/>
</context:component-scan>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有