1,使用Spring IOC与DI实现MVC的模拟例子:
spring配置文件:通过set方法进行注入,其实就是使用快捷键生成set方法,首先建立对应的全局变量(特别简单)。
把类放到Spring容器中的目的是为了创建对象,所以接口不应该放到Spring容器中。
使用注解做权限比较流行。
<!--让Spring容器帮助我们创建对象-->
<!--注入PersonDaoImpl类-->
<bean id="personDaoImpl" class="dao.PersonDaoImpl"/>
<!--注入PersonServiceImpl,让Spring帮助我们创建对象-->
<!--通过set方法对类中的属性进行赋值(属性的名称,好好理解这句话),因为属性是引用类型,所以使用ref-->
<bean id="personServiceImpl" class="service.PersonServiceImpl">
<property name="mPersonDaoImpl">
<ref bean="personDaoImpl"/>
</property>
</bean>
<!--personAction-->
<bean id="personAction" class="PersonAction">
<property name="mPersonService">
<ref bean="personServiceImpl"/>
</property>
</bean>
测试Spring mvc
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created on 2017/9/1.
* Author:crs
* Description:测试SpringMVC
*/
public class TestMvc {
@Test
public void testSpringMVC() {
//已经可以执行成功了,实现了面向接口编程。
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
PersonAction mPersonAction = (PersonAction) context.getBean("personAction");
mPersonAction.returnResult();
}
}
Action层:
import service.PersonService;
/**
* Created on 2017/9/1.
* Author:crs
* PersonAction层,通过spring实现mvc。
*/
public class PersonAction {
private PersonService mPersonService;
public PersonService getmPersonService() {
return mPersonService;
}
public void setmPersonService(PersonService mPersonService) {
this.mPersonService = mPersonService;
}
//获取查询的结果
public void returnResult() {
this.mPersonService.processLogic();
}
}
service层:
package service;
/**
* Created on 2017/9/1.
* Author:crs
* Description:PersonService
*/
public interface PersonService {
void processLogic();
}
package service;
import dao.PersonDaoImpl;
/**
* Created on 2017/9/1.
* Author:crs
* Description:PersonServiceImpl接口的实现类
*/
public class PersonServiceImpl implements PersonService {
private PersonDaoImpl mPersonDaoImpl;
public PersonDaoImpl getmPersonDaoImpl() {
return mPersonDaoImpl;
}
public void setmPersonDaoImpl(PersonDaoImpl mPersonDaoImpl) {
this.mPersonDaoImpl = mPersonDaoImpl;
}
public void processLogic() {
//调用dao层的方法,返回想要的结果。
this.mPersonDaoImpl.insertData();
}
}
dao层:
package dao;
/**
* Created on 2017/9/1.
* Author:crs
* Description:PersonDao 数据操作接口
*/
public interface PersonDao {
void insertData();
}
package dao;
/**
* Created on 2017/9/1.
* Author:crs
* Description:
*/
public class PersonDaoImpl implements PersonDao {
public void insertData() {
System.out.println("插入数据成功!");
}
}
2,自定义注解以及自定义注解解析器
package testAnnotation;
import java.lang.annotation.*;
/**
* Created on 2017/9/1.
* Author:crs
* Description:自定义类注解
*/
@Documented //此注解可以出现在帮助文档中
@Target(ElementType.TYPE) //此注解可以用在类上面
@Retention(RetentionPolicy.RUNTIME) //此注解编译和运行时发挥作用
public @interface ClassDescription {
//此注解存在一个String类型的value属性
String value();
}
package testAnnotation;
import java.lang.annotation.*;
/**
* Created on 2017/9/1.
* Author:crs
* Description:自定义一个使用在方法上的注解
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodDescription {
String name();
}
使用自定义注解:
package testAnnotation;
/**
* Created on 2017/9/1.
* Author:crs
* Description:测试自定义注解
*/
@ClassDescription("自己定义一个注解")
public class Test {
@MethodDescription(name = "自定义一个方法注解")
public void testJava() {
System.out.println("这是java学科!");
}
}
自定义注解解析器:
package testAnnotation;
import java.lang.reflect.Method;
/**
* Created on 2017/9/1.
* Author:crs
* Description:新建注解解析器,解析自定义注解
* 通过反射的方式,获取注解中的值
*/
public class ParseAnnotation {
@org.junit.Test
public void testParse() {
//获取类的字节码文件
Class<Test> testClass = Test.class;
//如果字节码文件中存在类注解
if (testClass.isAnnotationPresent(ClassDescription.class)) {
//获取此注解
ClassDescription annotation = testClass.getAnnotation(ClassDescription.class);
//打印注解的值
System.out.println(annotation.value());
}
//获取字节码文件中的方法集合
Method[] methods = testClass.getMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(MethodDescription.class)) {
MethodDescription annotation = method.getAnnotation(MethodDescription.class);
System.out.println(annotation.name());
}
}
}
}
3,Spring中的注解:@Resource注解
在Spring中,IOC与DI都有对应的注解,每个注解都用特定的用途。
分析注解使用的整个过程:
1、当Spring容器启动的时候,spring容器加载了配置文件
2、在Spring配置我呢间中,如果遇到<bean>的配置,就会为该类创建对象
3、在Spring容器的范围内查找bean,看哪些bean的属性上有@Resource注解
4、找到@Resource注解以后,判断该注解的属性是否为“”(name没有写)
如果没有写name属性,就会让属性的字段名称和bean中的id值进行匹配,如果匹配成功则赋值。
如果匹配不成功,就会按照属性类型来进行匹配;如果匹配不成功,就会报错。
如果有name属性,就会按照name属性的值和bean的id值进行匹配,如果匹配成功就赋值,匹配不成功就会报错。
@Autowired注解,应用于字段上,按照类型来进行装配对象,默认情况下它要求依赖对象必须存在
如何使用一个注解把类放到Spring容器中:类扫描注解解析器,普通的注解解析器
package springAnnotation;
import javax.annotation.Resource;
/**
* Created on 2017/9/1.
* Author:crs
* Description:Person
*/
public class Person {
@Resource(name = "student")
private Student student;
public void testShowStudent() {
student.sayHello();
}
}
package springAnnotation;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created on 2017/9/1.
* Author:crs
* Description:测试SpringAnnotation
* 如何进行Spring注解配置,让Spring容器支持注解的使用:导入命名空间,导入注解解析器<context:annotation-config></context:annotation-config>
* 基本数据类型不能使用注解,只有引用类型才能使用注解。
*
* @Resource注解的使用规则:
* 1)在配置文件中,进行注解配置,导入命名空间。
* 2)导入注解解析器
* 3)一般应用于类的属性上;该注解有一个name属性,默认为“”;
*/
public class TestSpringAnnotation {
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
Person person = (Person) context.getBean("person");
person.testShowStudent();
}
}
Spring注解配置文件:
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
<!--让Spring容器支持注解的使用-->
<context:annotation-config></context:annotation-config>
<!--测试Spring注解-->
<bean id="student" class="springAnnotation.Student"/>
<bean id="person" class="springAnnotation.Person"/>
4,类扫描注解解析器的使用规则:(需要进行扫描两次,@Component和@Resource)
作用:使用Spring注解完成bean的定义,不需要在xml文件中配置bean。
@Component的使用:只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean了。但是使用注解只能使用默认的构造函数创建对象。
定义bean时,id的值为类名,并且把第一个字母变成小写(规范)。
1)在spring的配置文件中导入命名空间
2)导入注解解析器;该注解解析器有两个功能:类扫描和依赖注入;在base-package包以及子包下查找所有的类
<!--类扫描注解解析器-->
<context:component-scan base-package="springAnnotation"/>
3)如果一个类上添加了@Component注解,就会进行如下的匹配
如果其value值的属性为"";
@Component
public class Person {
//这种注入bean的方式含义是:(@Component注解等价于下面这句话)
//<bean id="person" class="Person"/>
}
如果其value值的属性不为空;
@Component("a")
public class Person {
//这种注入bean的方式含义是:(@Component注解等价于下面这句话)
//<bean id="a" class="Person"/>
}
4)再次按照@Resource的法则进行匹配。
使用xml文件进行配置bean与使用注解进行配置bean的比较:
使用xml配置书写比较麻烦,但是效率高,不用多次扫描;使用注解进行配置书写简单,但是效率低。