注解,Soring注解方式实现IOC
构造方法装配
<constructor-arg"> </constructor-arg>type: 属性的类型 可以省略
index: 构造方法参数的索引
value: 是构造方法参数的实参
ref: 对自定义对象的引入
index,name 必须要有一个,如果没有要按顺序书写
com.wiscom.domain.Hero
package com.wiscom.domain; public class Hero { private int age; private String name; private Dog dog; private Cat cat; @Override public String toString() { return "Hero [age=" + age + ", name=" + name + ", dog=" + dog + ", cat=" + cat + "]"; } public Hero(int age, String name, Dog dog, Cat cat) { this.age = age; this.name = name; this.dog = dog; this.cat = cat; } public Hero() { } }
applicationContext
<bean id="hero" class="com.wiscom.domain.Hero"> <constructor-arg index="0" value="19"></constructor-arg> <constructor-arg name="name" value="迪迦"></constructor-arg> <constructor-arg index="2" type="com.wiscom.domain.Dog" ref="dog"></constructor-arg> <constructor-arg index="3" ref="cat"></constructor-arg> </bean> <bean id="dog" class="com.wiscom.domain.Dog"></bean> <bean id="cat" class="com.wiscom.domain.Cat"></bean>
自己写注解(@interface)
注解中的属性要加括号
只有一个属性时,且为value,使用时可以直接赋值
元注解
给注解 注解的注解
@Target 表示注解可以使用的位置
ElementType.TYPE接口、类、枚举、注解@Retention
RetentionPolicy.SOURCE仅存于源代码,calss文件中不包含
RetentionPolicy.CLASSclass文件中存在,运行时不包含
RetentionPolicy.RUNTIMEclass字节码中存在,运行时可以获取到@Documented
用来声明被修饰注解是否要被文档提取工具提取到文档中
默认不提取
@Inherited
被修饰的注解是否具有继承性
默认没有继承性
反射注解
isAnnotationPresent()
getAnnotation()
getAnnotations()
package com.wiscom.demo;
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; public class Demo { public static void main(String[] args) { // 判断Police类有没有Lever注解 if (Police.class.isAnnotationPresent(Lever.class)){ // 获取注解 Lever lever = Police.class.getAnnotation(Lever.class); // 获取注解的属性值 String str = lever.value(); if ("协警".equals(str)){ System.out.println("按照相关部门规定进行处罚"); }else if ("民警".equals(str)){ System.out.println("送200块钱"); }else if ("刑警".equals(str)){ System.out.println("牢底坐穿"); }else{ System.out.println("送50块"); } }else{ System.out.println("打一顿,扭送到派出所"); } } } @Target(ElementType.TYPE)//表示注解可以使用在类上面 @Retention(RetentionPolicy.RUNTIME)// 表示注解可以保留在运行阶段 @interface Lever{ String value(); } @Lever("民警") class Police{ }
Spring注解
需要导入spring-context-3.2.xsd约束文件
applicationContext.xml
<?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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd "> </beans>
Spring注解方式实现IOC
在spring的配置文件中,开启包扫描,指定spring自动扫描哪些个包下的类。只有在指定的扫描包下的类上的IOC注解才会生效。
第二个字母为大写,bean的id为原类名: UFO
第二个字母为小写,bean的id要小写第一个字母
applicationContext.xml
<!-- 开启包扫描 --> <context:component-scan base-package="com.wiscom.beans"></context:component-scan> com.wiscom.domain.Person package com.wiscom.domain; import org.springframework.stereotype.Component; @Component// <bean id="person" class="com.wiscom.domain.Person"></bean> public class Person { }
使用注解方式实现DI (不推荐使用)
使用后值无法改变
需要导入spring-util-3.2.xsd约束文件
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" 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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd " > <!-- 开启包扫描 --> <context:component-scan base-package="com.wiscom.domain"></context:component-scan> <!-- 开启注解方式配置DI --> <context:annotation-config></context:annotation-config> <util:list id="l1"> <value>讲师</value> <value>男朋友</value> <value>批卷老师</value> </util:list> <util:set id="s1"> <value>抽烟</value> <value>烫头</value> <value>槟榔</value> </util:set> <util:map id="m1"> <entry key="曹阳" value="se"></entry> <entry key="李帅" value="web"></entry> </util:map> <util:properties id="p1"> <prop key="朴乾">乱讲</prop> <prop key="赵栋">乱跑</prop> <prop key="兰刚">大数据</prop> </util:properties> </beans>
com.wiscom.domain.Hero
package com.wiscom.domain; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Hero { @Value("18") private int age; @Value("曹阳") private String name; @Value("#{@l1}") private List<String> jobs; @Value("#{@s1}") private Set<String> skills; @Value("#{@m1}") private Map<String,String>map; @Value("#{@p1}") private Properties prop; @Override public String toString() { return "Hero [age=" + age + ", name=" + name + ", jobs=" + jobs + ", skills=" + skills + ", map=" + map + ", prop=" + prop + "]"; } }
引入配置文件信息
<context:property-placeholder location="classpath:/person-data.properties"/>
data.properties
driverName=com.mysql.jdbc.Driver
url=jdbc:mysql:///springdb
name=root
password=root
com.wiscom.domain.MySqlConnector
package com.wiscom.domain; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class MySqlConnector { @Value("${driverName}") private String driverName; @Value("${url}") private String url; @Value("${name}") private String name; @Value("${password}") private String password; @Override public String toString() { return "MySqlConnector [driverName=" + driverName + ", url=" + url + ", name=" + name + ", password=" + password + "]"; } }
@Value
注入赋值
开启包扫描,开启注解配置方式
在使用注解前需要做的工作
<!-- 开启包扫描 -->
<context:component-scan base-package="com.wiscom.domain"></context:component-scan>
<!-- 开启注解方式配置DI -->
<context:annotation-config></context:annotation-config>
@Controller @Service @Repository @Component
用于将类控制反转到bean容器中
Controller 用于标识控制器
Service 用于标识服务类
Repository 用于标识持久层的类
@Autowired实现自定义bean类型的属性注入
标识类
先寻找id属性
再寻找属性类型
当标识接口时
先找id
再找属性类型
再在容器中找实现类,实现类只有一个时,自动注入
@Resource
自己设置bean容器的id,优先根据容器id进行查询
@Qualifier
该注解用于Service层,为了区分实现相同接口的不同类,选择有该注解的进行注入,达到解耦的目的
@Scope
配置修饰的类是单例还是多例
@lazy
配置修饰的类是否是懒加载
@PostConstruct
修饰类中的方法,申明这是一个初始化方法

浙公网安备 33010602011771号