spring 注解配置bean
1.TestObject类
package com.hxzy.annotation;
import org.springframework.stereotype.Component;
//此处的注解Component相当于一个声明,这样在bean配置文件中可以不用再定义bean对象,可直接在测试文件中使用testObject作为bean对象
@Component
public class TestObject {
}
2. UserController 类
package com.hxzy.annotation.controller;
import org.springframework.stereotype.Controller;
//模拟表现层 , 此处的注解同样不用在bean配置文件中配置bean对象,直接使用,使用时用userController
@Controller
public class UserController {
public void execute() {
System.out.println("UserController execute...");
}
}
3.
定义接口类
package com.hxzy.annotation.repository;
public interface UserRepository {
void save();
}
定义接口实现类
package com.hxzy.annotation.repository;
import org.springframework.stereotype.Repository;
//模拟持久化层 ,注意此处的注解包含有参数,可以在测试类中不使用userRepositoryImpl ,而是直接使用userRepository
@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository {
@Override
public void save() {
System.out.println("UserRepository save...");
}
}
4.服务类
package com.hxzy.annotation.service;
import org.springframework.stereotype.Service;
//模拟业务层,服务层
@Service
public class UserService {
public void add() {
System.out.println("UserService add...");
}
}
5.配置文件
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 指定spring ioc容器扫描的包,
指定 base-package(基目录) 后,清理工程,则 com.hxzy.annotation包中的所有类被自动扫描,并带有标示s。接口类除外
指定 resource-pattern ,表示指定扫描的文件为com.hxzy.annotation下repository目录中的所有class文件 -->
<!--
<context:component-scan
base-package="com.hxzy.annotation"
resource-pattern="repository/*.class">
</context:component-scan>
-->
<context:component-scan
base-package="com.hxzy.annotation"
use-default-filters="false"
>
<!-- context:exclude-filter : 不包含的包,注意expression的值不是全类名
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
-->
<!-- context:include-filter : 只包含指定的包,配置好后,运行无效,需要加use-default-filters="false"
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
-->
<!-- 采用全类名的方式过滤指定的类名, 此时删除掉 use-default-filters="false"
<context:exclude-filter type="assignable" expression="com.hxzy.annotation.repository.UserRepository"/>
-->
<!-- 采用全类名的方式 仅指定的类名可用 此处需要加上 use-default-filters="false" -->
<context:include-filter type="assignable" expression="com.hxzy.annotation.repository.UserRepository"/>
</context:component-scan>
</beans>
6.测试文件
package com.hxzy.annotation;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Repository;
import com.hxzy.annotation.controller.UserController;
import com.hxzy.annotation.repository.UserRepository;
import com.hxzy.annotation.service.UserService;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("annotation.xml");
//使用spring 的默认命名策略,testObject对象是基于TestObject类,将TestObject类的首字母小写,作为bean对象,不在配置文件中专门配置bean
TestObject to = (TestObject) ctx.getBean("testObject");
System.out.println(to);
//UserController类的首字母小写,作为bean对象
UserController userControl = (UserController) ctx.getBean("userController");
System.out.println(userControl);
UserService userService = (UserService) ctx.getBean("userService");
System.out.println(userService);
//在UserRepository类中注解若使用@Repository("userRepository"),则此处的ctx.getBean()中参数与注解中的参数相同
//否则若UserRepository类中注解若使用@Repository, 此处ctx.getBean("userRepositoryImpl")
UserRepository userRepository = (UserRepository) ctx.getBean("userRepository");
System.out.println(userRepository);
ctx.close();
}
}