package com.itheima10.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE) //该注解可以用于类上
@Retention(RetentionPolicy.RUNTIME) //在java,class文件以及运行时注解都起作用
@Documented //能生成在帮助文档中
public @interface ClassInfo {
/**
* 该注解有两个String类型的属性
* @return
*/
String name() default "";
String value() default "";
}
package com.itheima10.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD) //该注解可以用于方法上
@Retention(RetentionPolicy.RUNTIME) //在java,class文件以及运行时注解都起作用
@Documented //能生成在帮助文档中
public @interface MethodInfo {
/**
* 该注解有两个String类型的属性
*/
String name() default "";
String value() default "";
}
package com.itheima10.annotation;
@ClassInfo(name="小平果118",value="牛")
public class AnnotationUse {
@MethodInfo(name="java",value="spring框架很重要")
public void java(){
}
}
package com.itheima10.annotation;
import java.lang.reflect.Method;
import org.junit.Test;
public class AnnotationTest {
public static void test(){
/**
* 如果解析类的注解,先得到Class
* 如果解析方法的注解,先得到method
*/
Class class1 = Itheima10.class;
//判断类上面是否有ClassInfo注解
if(class1.isAnnotationPresent(ClassInfo.class)){
//得到类上面的注解
ClassInfo classInfo = (ClassInfo)class1.getAnnotation(ClassInfo.class);
System.out.println(classInfo.value());
System.out.println(classInfo.name());
}
Method[] methods = class1.getMethods();
for (Method method : methods) {
//正在遍历的方法上面是否存在MethodInfo注解
if(method.isAnnotationPresent(MethodInfo.class)){
MethodInfo methodInfo = method.getAnnotation(MethodInfo.class);
System.out.println(methodInfo.name());
System.out.println(methodInfo.value());
}
}
}
@Test
public void test(){
AnnotationTest.test();
}
}
package com.itheima10.spring.di.annotation;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
/**
* @Autowired//按照类型进行匹配
*
* @Autowired//按照类型进行匹配
@Qualifier("student")
*
*/
public class Person {
@Resource(name="student")
private Student student;
public void say(){
this.student.say();
}
}
package com.itheima10.spring.di.annotation;
public class Student {
public void say(){
System.out.println("student");
}
}
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!-- 把person和student放入到spring容器中 --> <bean id="person" class="com.itheima10.spring.di.annotation.Person"></bean> <bean id="student" class="com.itheima10.spring.di.annotation.Student"></bean> <!-- 引入context命名空间 xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" --> <!-- 启动了以来注入的注解解析器 --> <context:annotation-config></context:annotation-config> </beans>
package com.itheima10.spring.di.annotation;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 原理:
* 1、启动spring容器
* 2、把person和student两个bean实例化
* 3、当spring容器解析到
* <context:annotation-config></context:annotation-config>
* 就会启动依赖注入的注解解析器
* 4、spring容器会在纳入spring管理的bean的范围内查找,看这些类的哪些属性上加有@Resource注解
* 5、如果某一个属性上加有@Resource注解
* 会查看该注解的name属性的值是否为""
* 如果为"",则会把该注解所在的属性的名称和spring容器中的id的值作匹配,如果匹配成功,则赋值
* 如果匹配不成功,则按照类型进行匹配,匹配成功则赋值
* 如果再匹配不成功,则报错
* 如果不为"",则把该注解的name属性的值和spring容器中id的值作匹配,如果匹配成功,则赋值
* 如果匹配不成功,则直接报错
*
说明:
注解只能作用于引用类型
xml与注解的对比
xml的效率比较高,书写比较麻烦
注解的书写比较简单,效率比较低
*
*/
public class AnnotationTest {
@Test
public void testAnnotation(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person)context.getBean("person");
person.say();
}
}
@Component
public class Person {
@Resource(name="student")
private Student student;
public void say(){
this.student.say();
}
}
@Component
public class Student {
public void say(){
System.out.println("student");
}
}
<!-- component 组件 把一个类放入到spring容器中,该类就称为组件 在base-package指定的包及子包下扫描 --> <context:component-scan base-package="com.itheima10.spring.scan"></context:component-scan>
/**
* 原理
* 1、启动spring容器
* 2、spring容器解析
* <context:component-scan base-package="com.itheima10.spring.scan">
</context:component-scan>
3、在base-package指定的包及子包中扫描,看哪些类上面是否含有@Component注解
4、如果有该注解
@Component
public class Person {
}
==等价于
<bean id="person" class="..Person">
@Component("aa")
public class Person {
}
==等价于
<bean id="aa" class="..Person">
5、按照@Resource的解析步骤执行
说明:
整个过程扫描两次,效率越来越低,书写越来越简单
*
*
*/
public class AnnotationTest {
@Test
public void testAnnotation(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person)context.getBean("person");
person.say();
}
}
@Component("excelDocument")
public class ExcelDocument implements Document{
public void read() {
System.out.println("excel read");
}
public void write() {
System.out.println("excel write");
}
}
@Component("pdfDocument")
public class PDFDocument implements Document{
public void read() {
System.out.println("pdf read");
}
public void write() {
System.out.println("pdf write");
}
}
@Component("wordDocument")
public class WordDocument implements Document{
public void read() {
System.out.println("word read");
}
public void write() {
System.out.println("word write");
}
}
@Component("documentManager")
public class DocumentManager {
@Resource(name="excelDocument")
private Document document;
public void read(){
this.document.read();
}
public void write(){
this.document.write();
}
}
public class DocumentTest {
@Test
public void testDocument(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DocumentManager documentManager = (DocumentManager)context.getBean("documentManager");
documentManager.read();
documentManager.write();
}
}
@Repository("personDao")
public class PersonDaoImpl implements PersonDao {
@Override
public void savePerson() {
System.out.println(" save person");
}
}
@Service("personService")
public class PersonServiceImpl implements PersonService{
@Resource(name="personDao")
private PersonDao personDao;
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
@Override
public void savePerson() {
this.personDao.savePerson();
}
}
@Controller("personAction")
public class PersonAction {
@Resource(name="personService")
private PersonService personService;
public void setPersonService(PersonService personService) {
this.personService = personService;
}
public void savePerson(){
this.personService.savePerson();
}
}
public class MVCTest {
@Test
public void testMVC(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
PersonAction personAction = (PersonAction)context.getBean("personAction");
personAction.savePerson();
}
}
<bean id = "bean1" class = "……TestBean"> <property name="sex" value="male"/> </bean> <bean id = "bean2" class = "……TestBean"> <property name="sex" value="male"/> </bean> <bean id = "bean3" class = "……TestBean"> <property name="sex" value="female"/> </bean>
<bean id ="BaseBean" class ="……TestBean"> <property name="sex" value="male"/> </bean>
<bean id ="bean1" parent = "BaseBean"/> 继承父Bean的属性 <bean id ="bean2" parent = "BaseBean"/> <bean id ="bean3" parent = "BaseBean"> 覆盖父Bean的属性 <property name="sex" value="female"/> </bean>
<bean id = "bean1" class = "……ATestBean"> <property name="sex" value="male"/> <property name="task" ref="task"/> </bean> <bean id = "bean2" class = "……BTestBean"> <property name="sex" value="male"/> </bean>
<bean id = "baseSex" abstract="true"> <property name="sex" value="male"/> </bean>
<bean id = "bean1" class = "……ATestBean" parent="baseSex"> <property name="task" ref="task"/> </bean> <bean id = "bean2" class = "……BTestBean" parent="baseSex"/>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有