做网站要会哪些软件,太原要做网站的公司,越秀微网站建设,福州网页模板建站本文主要包括以下内容
注解
注解
1、注解就是为了说明java中的某一个部分的作用(Type) 2、注解都可以用于哪个部门是Target注解起的作用 3、注解可以标注在ElementType枚举类所指定的位置上 4、 Documented //该注解是否出现在帮助文档中 Retention(Retenti…本文主要包括以下内容
注解
注解
1、注解就是为了说明java中的某一个部分的作用(Type) 2、注解都可以用于哪个部门是Target注解起的作用 3、注解可以标注在ElementType枚举类所指定的位置上 4、 Documented //该注解是否出现在帮助文档中 Retention(RetentionPolicy.RUNTIME) //该注解在java,class和运行时都起作用 Target(ElementType.ANNOTATION_TYPE)//该注解只能用于注解上 public interface Target { ElementType[] value(); }
5、用来解析注解的类成为注解解析器
自定义注解实例
类注解
package cn.itcast.annotation.sh;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;Documented
Retention(RetentionPolicy.RUNTIME)
Target(ElementType.TYPE)
public interface Description {String value();
}方法注解
package cn.itcast.annotation.sh;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;Documented
Retention(RetentionPolicy.RUNTIME)
Target(ElementType.METHOD)
public interface MethodDesc {String name();
}
注解的使用
package cn.itcast.annotation.sh;Description(whuhan university)
public class ITCAST_SH {MethodDesc(namewhu)public void java(){System.out.println(this is java);}
}
注解解析
package cn.itcast.annotation.sh;import java.lang.reflect.Method;import org.junit.Test;public class AnnotationParse {Testpublic void testParse(){Class class1 ITCAST_SH.class;if(class1.isAnnotationPresent(Description.class)){Description description (Description)class1.getAnnotation(Description.class);if(description.value().contains(whuhan)){System.out.println(学费减半);}}Method[] methods class1.getMethods();for(Method method:methods){if(method.isAnnotationPresent(MethodDesc.class)){MethodDesc methodDesc (MethodDesc)method.getAnnotation(MethodDesc.class);if(methodDesc.name().contains(whu)){System.out.println(折上折);}}}}
}
Resource注解的使用(依赖注入)
1、在spring的配置文件中导入命名空间 http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd
2、引入注解解析器 context:annotation-config/context:annotation-config
3、在spring的配置文件中把bean引入进来 4、在一个类的属性上加 Resource(namestudent_annotation)private Student student;从该注解本身Target({TYPE, FIELD, METHOD})Retention(RUNTIME)public interface Resource {String name() default ;}
1、该注解可以用于属性上或者方法上但是一般用户属性上 2、该注解有一个属性name,默认值为”” 5、分析整个过程 1、当启动spring容器的时候spring容器加载了配置文件 2、在spring配置文件中只要遇到bean的配置就会为该bean创建对象 3、在纳入spring容器的范围内查找所有的bean,看哪些bean的属性或者方法上加有Resource 4、找到Resource注解以后判断该注解name的属性是否为”“(name没有写) 如果没有写name属性则会让属性的名称的值和spring中ID的值做匹配如果匹配成功则赋值 如果匹配不成功则会按照类型进行匹配如果匹配不成功则报错 如果有name属性则会按照name属性的值和spring的bean中ID进行匹配匹配成功则赋值不成功则报错
实例
package cn.itcast.spring.annotation.di;public class Student {public void show(){System.out.println(student);}
}
Person.java
package cn.itcast.spring.annotation.di;import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;public class Person {private Long pid;private String pname;Resource(namestudent_annotation)
// Autowired
// Qualifier(student)private Student student;private Set sets;private List lists;private Map map;private Properties properties;public void showStudent(){this.student.show();}
}XML文件
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/contextxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd!-- 注解解析器--context:annotation-config/context:annotation-configbean idperson_annotation classcn.itcast.spring.annotation.di.Person/beanbean idstudent_annotation classcn.itcast.spring.annotation.di.Student/bean
/beans
类扫描的注解Component使用
1、在spring的配置文件中导入命名空间 http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd
2、加入下面的语句
context:component-scan base-packagecn.itcast.annotation.scan/context:component-scan 1、 该注解解析器包含了两个功能依赖注入和类扫描 2、在base-package包及子包下查找所有的类 3、如果一个类上加了Component注解就会进行如下的法则 如果其value属性的值为 Componentpublic class Person {}bean idperson class..Person如果其value属性的值不为Component(p)public class Person {}bean idp class..Person
4、按照Resource的法则再次进行操作
实例
package cn.itcast.annotation.scan;import org.springframework.stereotype.Component;Component(b)
//bean idb class..Student
public class Student {public void show(){System.out.println(student);}
}
person.java
package cn.itcast.annotation.scan;import javax.annotation.Resource;import org.springframework.stereotype.Component;Component(a)
//bean ida class..Person
public class Person {Resource(nameb)private Student student;public void showStudent(){this.student.show();}
}
XML文件的配置
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/contextxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd!-- 类扫描的注解解析器component 指的就是一个类base-package 在该包及子包中进行扫描--context:component-scan base-packagecn.itcast.annotation.scan/context:component-scan
/beans
新特性
Spring 2.5引入了更多典型化注解(stereotype annotations) Component、Service和 Controller。 Component是所有受Spring管理组件的通用形式 而Repository、Service和 Controller则是Component的细化 用来表示更具体的用例(例如分别对应了持久化层、服务层和表现层)。也就是说 你能用Component来注解你的组件类 但如果用Repository、Service或Controller来注解它们你的类也许能更好地被工具处理或与切面进行关联。 例如这些典型化注解可以成为理想的切入点目标。当然在Spring Framework以后的版本中Repository、Service和 Controller也许还能携带更多语义。如此一来如果你正在考虑服务层中是该用 Component还是Service 那Service显然是更好的选择。同样的就像前面说的那样 Repository已经能在持久化层中进行异常转换时被作为标记使用了。
示例
PersonAction(控制层)
package cn.itcast.annotation.mvc;import javax.annotation.Resource;import org.springframework.stereotype.Controller;Controller(personAction)
public class PersonAction {Resource(namepersonService)private PersonService personService;public void savePerson(){this.personService.savePerson();}
}
PersonServiceImpl(service层)
package cn.itcast.annotation.mvc;import javax.annotation.Resource;import org.springframework.stereotype.Service;Service(personService)
public class PersonServiceImpl implements PersonService{Resource(namepersonDao)private PersonDao personDao;Overridepublic void savePerson() {// TODO Auto-generated method stubthis.personDao.savePerson();}}
PersonDaoImpl(dao层)
package cn.itcast.annotation.mvc;import org.springframework.stereotype.Repository;Repository(personDao)
public class PersonDaoImpl implements PersonDao{Overridepublic void savePerson() {// TODO Auto-generated method stubSystem.out.println(save person dao);}}
xml与注解
1、xml书写麻烦但是效率高 2、注解书写简单但是效率低
完成