一、通用注解
1、项目结构:
2、新建Person类,注解@Component未指明id,则后期使用spring获取实例对象时使用默认id="person"方式获取或使用类方式获取
package hjp.spring.annotation.commen;import org.springframework.stereotype.Component;//@Component @Component("personId") public class Person { private String name; private Integer age; public String getName() {return name; } public void setName(String name) {this.name = name; } public Integer getAge() {return age; } public void setAge(Integer age) {this.age = age; } @Override public String toString() {return "Person [name=" + name + ", age=" + age + "]"; } }
3、新建beans.xml文件,相比之前配置多了
xmlns:context="http://www.springframework.org/schema/context"
和
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
不再使用bean节点配置,而是使用context:component-scan节点,属性base-package指明要扫描注解的包
<?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.xsd"><!-- <bean id="personId" class="hjp.spring.annotation.commen.Person"></bean> --><context:component-scan base-package="hjp.spring.annotation.commen"></context:component-scan> </beans>
4、新建测试类
package hjp.spring.annotation.commen;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component;public class TestApp {@Testpublic void demo1() {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("hjp/spring/annotation/commen/beans.xml");//注解未指定id时,默认id为person//Person person = applicationContext.getBean("person", Person.class); //注解指定id,即@Component("personId")时 //Person person = applicationContext.getBean("personId",Person.class);//不管是否指定id,使用类方式获取实例都可以Person person = applicationContext.getBean(Person.class);System.out.println(person);} }
二、衍生三层开发注解
@Controller 修饰Web层;@Service 修饰service层;@Repository 修饰dao层
依赖注入:方式1、普通数据 @Value
方式2、引用数据 @Autowired,默认按照类型进行注入;如果想要按照名称进行注入,还需要使用注解@Qualifier("名称")
1、项目结构
2、新建UserDao类
package hjp.spring.annotation.web;import org.springframework.stereotype.Repository;//@Repository @Repository("userDaoId") public class UserDao {public void save() {System.out.println("add user");} }
3、新建UserService类
package hjp.spring.annotation.web;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service;@Service public class UserService {@Autowired//@Qualifier("userDaoId") //指向UserDao类注解@Repository("userDaoId")指定的Id//属性注入也可以使用注解@Resource//@Resourceprivate UserDao userDao;public void setUserDao(UserDao userDao) {this.userDao = userDao;}public void addUser() {userDao.save();} }
4、新建UserAction类
package hjp.spring.annotation.web;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller;@Controller("userActionId") //@Scope("prototype")//多例 public class UserAction {@Autowiredprivate UserService userService;public void setUserService(UserService userService) {this.userService = userService;}public void execute() {userService.addUser();} }
5、新建测试类
package hjp.spring.annotation.web;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestApp {@Testpublic void demo1() {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("hjp/spring/annotation/web/beans.xml");UserAction userAction = applicationContext.getBean("userActionId", UserAction.class);System.out.println(userAction);//用于测试单例和多例 userAction.execute();userAction=applicationContext.getBean("userActionId", UserAction.class);System.out.println(userAction);//用于测试单例和多例 } }
6、新建beans.xml配置文件,内容和通用注解一样,只是修改下扫描包的地址
<?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.xsd"><!-- <bean id="personId" class="hjp.spring.annotation.commen.Person"></bean> --><!-- 让spring对含有注解的类进行扫描 --><context:component-scan base-package="hjp.spring.annotation.web"></context:component-scan> </beans>
三、其他注解:如@PostConstruct修饰初始化;@PreDestroy修饰销毁
四、项目中对XML和注解的使用情况
1、纯XML,整合第三方(jar包)
2、纯注解,限制条件必须有源码,简化代码开发
3、xml+注解,xml配置bean,代码中使用注入注解
如果混合使用不需要扫描,只需要加入<context:annotation-config></context:annotation-config>配置
测试Demo可以将UserDao类的@Repository、UserService类的@Service、UserAction类的@Controller去掉,只保留注入注解,
然后修改beans.xml文件为
<?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.xsd"><!-- 只使用注入的注解 --><bean id="userDaoId" class="hjp.spring.annotation.web.UserDao"></bean><bean id="userServiceId" class="hjp.spring.annotation.web.UserService"></bean><bean id="userActionId" class="hjp.spring.annotation.web.UserAction"></bean><context:annotation-config></context:annotation-config> </beans>