1.利用注解注入信息.
package cn.itcast.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Inherited
public @interface DbInfo {
String url() default "jdbc:mysql://localhost:3306/test";
String username() default "root";
String password() default "root";
}
package cn.itcast.annotation;
import java.sql.Connection;
public class JdbcUtils {
@DbInfo(url="jdbc:mysql://localhost:3306/test",username="flx",password="root")
public static Connection getConnection(String url,String username,String password){
System.out.println(url);
System.out.println(username);
System.out.println(password);
return null;
}
}
package cn.itcast.annotation;
import java.lang.reflect.Method;
public class Demo1 {
/**
* @param args
* @throws Exception
* @throws SecurityException
*/
public static void main(String[] args) throws SecurityException, Exception {
Class clazz = JdbcUtils.class;
Method method = clazz.getMethod("getConnection", String.class,
String.class, String.class);
DbInfo di = method.getAnnotation(DbInfo.class);
String url = di.url();
String username = di.username();
String password = di.password();
method.invoke(null, url, username, password);
}
}
注入对象,又分为两种情况:1)在方法上注入,2)在属性上注入.
Test为在方法上注入 Test2为在属性上注入
package cn.itcast.annotation2;
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package cn.itcast.annotation2;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface InjectPerson {
String name();
int age();
}
package cn.itcast.annotation2;
public class PersonDao {
@InjectPerson(name="zhangsan",age=23) private Person person;
public Person getPerson() {
return person;
}
@InjectPerson(name="lisi",age=23)
public void setPerson(Person person) {
this.person = person;
}
}
package cn.itcast.annotation2;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) throws Exception {
// 1.得到要注入的属性
PropertyDescriptor pd = new PropertyDescriptor("person",PersonDao.class);
// 2.得到要注入的属性需要的类型
Class clazz = pd.getPropertyType(); // Person
// 3.创健属性需要的对象
Object person = clazz.newInstance();
// 4.得到属性的写方法
Method setPerosn = pd.getWriteMethod();
// 5.反射出方法上声明的注解
InjectPerson inject = setPerosn.getAnnotation(InjectPerson.class);
// 6.得到注解上声明的信息,填充person对象
Method[] methods = inject.getClass().getMethods();
for (Method m : methods) {
String methodName = m.getName(); // name or age
try {
Field f = Person.class.getDeclaredField(methodName);
Object value = m.invoke(inject, null); // 得到注解上配置的属性的值
f.setAccessible(true);
f.set(person, value);
} catch (Exception e) {
continue;
}
}
// 7.把填充了数据的person通过setPerson方法整到personDao对象上
PersonDao dao = new PersonDao();
setPerosn.invoke(dao, person);
System.out.println(dao.getPerson().getName());
System.out.println(dao.getPerson().getAge());
}
}
package cn.itcast.annotation2;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test2 {
public static void main(String[] args) throws Exception {
// 1.得到需要注入的属性
Field f = PersonDao.class.getDeclaredField("person");
// 2.得到属性需要的 类型
Class clazz = f.getType();
// 3.创建person
Person person = (Person) clazz.newInstance();
// 4.反射属性的注解,
InjectPerson inject = f.getAnnotation(InjectPerson.class);
// 5.并用注解的信息填充person
Method ms[] = inject.getClass().getMethods();
for (Method m : ms) {
String methodName = m.getName(); // name( ) age()
// 看person对象上有没有注解与之对应的属性
try {
PropertyDescriptor pd = new PropertyDescriptor(methodName,
Person.class);
Method set = pd.getWriteMethod(); // setname setage
set.invoke(person, m.invoke(inject, null));
} catch (Exception e) {
continue;
}
}
// 6.把person赋给dao
PersonDao dao = new PersonDao();
f.setAccessible(true); // person
f.set(dao, person);
System.out.println(dao.getPerson().getName());
System.out.println(dao.getPerson().getAge());
}
}