一、使用注解的DI实现
1.@Resource
使用该注解能够实现引用型属性的DI实现,该注解能够根据属性名和属性类型自动给属性赋值。一般使用@Resource(name=”student”)的形式明确指定名称,这样就只能根据属性名来填充值了。
步骤:
(1)导入命名空间:命名空间示例
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config/> </beans>
applicationContext.xml
(2)导入依赖注入的注解解析器
<context:annotation-config></context:annotation-config>
(3)将bean导入进来,例:
<bean id="person" class="com.kdyzm.spring.di.Person"></bean>
<bean id="student" class="com.kdyzm.spring.di.Student"></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
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<!-- 该标签位置不能改变 -->
<context:annotation-config></context:annotation-config>
<!-- 使用该注解的话不需要再在xml文件中进行配置了 -->
<bean id="person" class="com.kdyzm.spring.di.Person"></bean>
<bean id="student" class="com.kdyzm.spring.di.Student"></bean>
</beans>
注意事项:如果类中包含标准类型的属性变量,同时需要spring该该属性赋初值,那么该类就不能使用注解的形式给属性赋初值了。
2.@PostConstruct
该注解表名init方法,相当于bean标签的init-method属性的功能。
3.@PreDestroy
该注解表名destroy方法,相当于bean标签的destory-method属性的功能。
4.@Autowired
该注解是spring的注解,能够根据类型自动匹配。
5.@Qualifier(有疑问,注解的使用有问题)
该注解也是spring的注解,能够根据名称自动匹配
也就是说Autowired注解和Qualifier注解两个注解的功能合起来才能够顶的上Resource注解的功能,所以对于这两个注解,只是需要知道就行了,不需要进行掌握。一般使用Resource注解就足够用了。
6.@Component
该注解是spring的注解;用于扫描类并将其纳入spring容器中管理。功能相当于xml中的bean标签。
配置方式:同样需要导入依赖注入的注解解析器
<context:component-scan base-package="com.kdyzm.spring.annotation.scan"></context:component-scan>
@Component可以细分为三个部分:@Repository、@Service、@Controller,对应着持久化层、服务层、控制层,用法和@Component相同,推荐使用后者,原因是被注解的类或许能够更好地被工具处理或者与切面进行关联。
二、spring中的继承
1.使用普通方法的继承创建子类对象之后不能继承父类的成员变量,调用父类的get方法也不能获取父类的成员变量。指定bean的parent属性能够解决该问题。
<bean id="person" class="com.kdyzm.spring.extend.Person">
<property name="name" value="Person"></property>
</bean>
<bean id="student" class="com.kdyzm.spring.extend.Student" parent="person"></bean>
2.使用abstract属性标记bean为抽象bean,这样spring就不会实例化该bean了。
<bean id="person" class="com.kdyzm.spring.extend.Person" abstract="true">
<property name="name" value="Person"></property>
</bean>
疑问:abstract属性默认值是true?并不管用,仍然实例化了。
三、动态代理复习:伪模拟hibernate的过程实现
1.动态代理的本质实际上就是方法增强
实现了InvocationHandler接口的类相当于一个“拦截器”。
2.实例。
package com.kdyzm.spring.proxy; public class Transaction {
public void startTransaction(){
System.out.println("开启事务!");
}
public void commit(){
System.out.println("提交事务!");
}
}
com.kdyzm.spring.proxy.Transaction.java
package com.kdyzm.spring.proxy; public class Person {
private String name; public Person(String name) {
this.name = name;
} public Person() {
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "Person [name=" + name + "]";
}
}
com.kdyzm.spring.proxy.Person.java
package com.kdyzm.spring.proxy; public interface PersonDao {
public void savePerson();
public Person getPerson();
public void updatePerson();
public void deletePerson();
}
com.kdyzm.spring.proxy.PersonDao.java
package com.kdyzm.spring.proxy; public class PersonDaoImpl implements PersonDao { @Override
public void savePerson() {
System.out.println("保存学生!");
} @Override
public Person getPerson() {
Person p=new Person();
p.setName("狗蛋");
return p;
} @Override
public void updatePerson() {
System.out.println("更新学生信息!");
} @Override
public void deletePerson() {
System.out.println("删除学生!");
} }
com.kdyzm.spring.proxy.PersonDaoImpl.java
最重要的一个类:拦截器类com.kdyzm.spring.proxy.PersonDaoInterceptor.java,实现了InvocationHandler接口
package com.kdyzm.spring.proxy; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method; /*
* 定义拦截器,这是核心类。
*/
public class PersonDaoInterceptor implements InvocationHandler{
private Transaction transaction;
private Object target;
public PersonDaoInterceptor(Object target,Transaction transaction) {
this.target=target;
this.transaction=transaction;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
String methodName = method.getName();
Object result=null;
if("savePerson".equals(methodName)||"updatePerson".equals(methodName)||"deletePerson".equals(methodName)){
this.transaction.startTransaction();
result=method.invoke(this.target, args);
this.transaction.commit();
}
else{
result=method.invoke(this.target, args);
}
return result;
} }
测试类:com.kdyzm.spring.proxy.Test.java
package com.kdyzm.spring.proxy; import java.lang.reflect.Proxy; public class Test {
public static void main(String[] args) {
PersonDao target=new PersonDaoImpl();
Transaction transaction = new Transaction();
PersonDaoInterceptor daoInterceptor = new PersonDaoInterceptor(target, transaction);
PersonDao proxy=(PersonDao) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), daoInterceptor);
Person p=proxy.getPerson();
System.out.println(p.getName());
proxy.savePerson();
}
}
运行结果: