首页 技术 正文
技术 2022年11月14日
0 收藏 854 点赞 3,899 浏览 5288 个字

一、使用注解的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();
}
}

运行结果:

    【Java EE 学习 50】【Spring学习第二天】【使用注解的DI实现】【spring中的继承】【动态代理伪hibernate实现】

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295