注解方式的bean装配
一、注解的介绍
- 代码里面的特殊标记,使用注解可以完成功能;
- 注解写法:@注解名称(属性名称=属性值);
- 注解可以使用在类、属性和方法上面;
一、注解开发的准备
导入基本jar包+AOP的jar包
beans,core,expression,context,log4j,commons-longging,aop创建xml配置文件,且添加schema的约束,并且配置XML文件开启注解扫描;
创建对象的注解有四种,目前的功能相同,后续可能会有所扩展;
- @Component
- @Controller
- @Service
- @Reqository
创建对象单实例还是多实例
- @Scope默认是单实例的;
- @Scope(value=”prototype”)表示为多实例的
三、注解实现bean的装配(实例)
- 创建xml配置文件,且添加schema的约束,并且配置XML文件开启注解扫描;
<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 definitions here -->
<!-- 开启注解扫描,在包里面扫描类、方法、属性上面是否有注解 -->
<context:component-scan base-package="com.spring.annotation"></context:component-scan>
</beans>
- 创建User类,并且添加注解
package com.spring.annotation;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Component(value="user")
public class User {
public void add(){
System.out.println("this is user add....."+name);
}
}
- 写测试代码
package test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.annotation.User;
public class AnnotationUser {
@Test
public void testAnnotation(){
ApplicationContext context = new ClassPathXmlApplicationContext("annotationBean.xml");
User user = (User) context.getBean("user");
System.out.println(user);
user.add();
}
}
- 输出结果
com.spring.annotation.User@35a50a4c
this is user add.....null
四、注解注入属性
创建xml配置文件,且添加schema的约束,并且配置XML文件开启注解扫描,同上;
创建User类和UserService类,使用注解方式时,可以不创建setter方法,@AutoWired注解是根据对象类型自动注入的;@Resourse(name=”user”)注解必须添加name属性,而且是根据对象名称完成注入的。
User类
package com.spring.annotation;
import java.util.Properties;
import org.springframework.stereotype.Component;
@Component(value="user")
public class User {
private String name="limi";
public void setName(String name) {
this.name = name;
}
public void add(){
System.out.println("this is user add....."+name);
}
}
UserService类
package com.spring.annotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component("userService")
public class UserService {
//@Autowired //用注解注入对象属性,根据对象类型查找
@Resource(name="user12") //根据对象名称查找
private User user;
public void print(){
System.out.println("UserService print:"+user);
user.add();
}
}
- 写测试代码
@Test
public void testService(){
ApplicationContext context = new ClassPathXmlApplicationContext("annotationBean.xml");
UserService userService = (UserService) context.getBean("userService");
System.out.println(userService);
userService.print();
}
- 输出结果
com.spring.annotation.UserService@134593bf
UserService print:com.spring.annotation.User@4bb4de6a
this is user add.....limi
注解和配置文件混合使用
- 使用配置文件开启注解扫描,及对象bean的装配;
- 使用注解完成属性的注入。