目錄
1.什麼是IOC
2.IOC入門小練習(開口小菜)
2.1 基于properties配置檔案方式
2.2練習二判斷一個類和屬性是否包含某一個注解以及擷取注解的value
3,手敲IOC容器以及DI(正片開始)
1.什麼是IOC
IOC(Inversion of Control),控制反轉。
就是指将對象的建立,對象的存儲(map),對象的管理(依賴查找,依賴注入)交給了spring容器。
最要要的作用就是解耦合
2.IOC入門小練習(開口小菜)
2.1 基于properties配置檔案方式
properties配置檔案
pojo=cn.tedu.test.Pojo
上代碼
具體的實作類
package cn.tedu.myspring_two;
import cn.tedu.test.Pojo;
import java.io.FileInputStream;
import java.util.Properties;
/**
* 基于properties配置檔案方式 擷取bean
*/
public class TestSpring {
public static void main(String[] args) {
try {
Properties properties = new Properties();//properties屬于map集合 存方的都是key=value的形式
properties.load(new FileInputStream("src\\cn\\tedu\\myspring_two\\spring.properties"));//讀入properties檔案
String pojo = properties.getProperty("pojo");//擷取key為pojo的對象
Class<?> aClass = Class.forName(pojo);//通過反射擷取位元組碼檔案
Object o = aClass.newInstance();//将位元組碼檔案執行個體化
Pojo o1 = (Pojo) o;//強轉成為子類類型
System.out.println(o1);
}catch (Exception e){
e.printStackTrace();
}
}
}
2.2練習二判斷一個類和屬性是否包含某一個注解以及擷取注解的value
上代碼
自定義Aut注解
package cn.tedu.myspring_three;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Aut {
String value()default "Aut";
}
自定義Service注解
package cn.tedu.myspring_three;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Service {
String value() default "service";
}
建立測試類SpringTest
package cn.tedu.myspring_three;
import java.lang.reflect.Field;
public class SpringTest {
public void method() throws ClassNotFoundException {
Class<?> aClass = Class.forName("cn.tedu.myspring_three.Student");//擷取位元組碼檔案
if (aClass.isAnnotationPresent(Service.class)) {//判斷位元組碼是否辦函Service類
System.out.println("這個類包含Service注解");
}
Field[] declaredFields = aClass.getDeclaredFields();//擷取私有屬性
for (Field declaredField : declaredFields) {//周遊私有屬性
if (declaredField.isAnnotationPresent(Aut.class)) {//判斷屬性上是否包含Aut注解
System.out.println(declaredField.getName()+"屬性包含"+"Aut"+"注解");
}else {
System.out.println(declaredField.getName() + "屬性" + "不包含Aut注解");
}
}
}
public static void main(String[] args) throws ClassNotFoundException {//測試
new SpringTest().method();
}
}
3,手敲IOC容器以及DI(正片開始)
建立自定義注解CGB
package cn.tedu.myspring;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface CGB {
String value() default"";
}
自定義value注解
package cn.tedu.myspring;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface value {
String value()default "";
}
建立工具類MyTools,用來擷取包裡面的所有類的位元組碼
package cn.tedu.myspring;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
* 本類是一個掃描報的工具類
*/
public class MyTools {
public static Set<Class<?>> getClasses(String pack) {
// 第一個class類的集合
Set<Class<?>> classes = new LinkedHashSet<Class<?>>();
// 是否循環疊代
boolean recursive = true;
// 擷取包的名字 并進行替換
String packageName = pack;
String packageDirName = packageName.replace('.', '/');
// 定義一個枚舉的集合 并進行循環來處理這個目錄下的things
Enumeration<URL> dirs;
try {
dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName);
// 循環疊代下去
while (dirs.hasMoreElements()) {
// 擷取下一個元素
URL url = dirs.nextElement();
// 得到協定的名稱
String protocol = url.getProtocol();
// 如果是以檔案的形式儲存在伺服器上
if ("file".equals(protocol)) {
// 擷取包的實體路徑
String filePath = URLDecoder.decode(url.getFile(), "UTF-8");
// 以檔案的方式掃描整個包下的檔案 并添加到集合中
findClassesInPackageByFile(packageName, filePath, recursive, classes);
} else if ("jar".equals(protocol)) {
// 如果是jar封包件
// 定義一個JarFile
System.out.println("jar類型的掃描");
JarFile jar;
try {
// 擷取jar
jar = ((JarURLConnection) url.openConnection()).getJarFile();
// 從此jar包 得到一個枚舉類
Enumeration<JarEntry> entries = jar.entries();
findClassesInPackageByJar(packageName, entries, packageDirName, recursive, classes);
} catch (IOException e) {
// log.error("在掃描使用者定義視圖時從jar包擷取檔案出錯");
e.printStackTrace();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return classes;
}
private static void findClassesInPackageByJar(String packageName, Enumeration<JarEntry> entries, String packageDirName, final boolean recursive, Set<Class<?>> classes) {
// 同樣的進行循環疊代
while (entries.hasMoreElements()) {
// 擷取jar裡的一個實體 可以是目錄 和一些jar包裡的其他檔案 如META-INF等檔案
JarEntry entry = entries.nextElement();
String name = entry.getName();
// 如果是以/開頭的
if (name.charAt(0) == '/') {
// 擷取後面的字元串
name = name.substring(1);
}
// 如果前半部分和定義的包名相同
if (name.startsWith(packageDirName)) {
int idx = name.lastIndexOf('/');
// 如果以"/"結尾 是一個包
if (idx != -1) {
// 擷取包名 把"/"替換成"."
packageName = name.substring(0, idx).replace('/', '.');
}
// 如果可以疊代下去 并且是一個包
if ((idx != -1) || recursive) {
// 如果是一個.class檔案 而且不是目錄
if (name.endsWith(".class") && !entry.isDirectory()) {
// 去掉後面的".class" 擷取真正的類名
String className = name.substring(packageName.length() + 1, name.length() - 6);
try {
// 添加到classes
classes.add(Class.forName(packageName + '.' + className));
} catch (ClassNotFoundException e) {
// .error("添加使用者自定義視圖類錯誤 找不到此類的.class檔案");
e.printStackTrace();
}
}
}
}
}
}
private static void findClassesInPackageByFile(String packageName, String packagePath, final boolean recursive, Set<Class<?>> classes) {
// 擷取此包的目錄 建立一個File
File dir = new File(packagePath);
// 如果不存在或者 也不是目錄就直接傳回
if (!dir.exists() || !dir.isDirectory()) {
// log.warn("使用者定義包名 " + packageName + " 下沒有任何檔案");
return;
}
// 如果存在 就擷取包下的所有檔案 包括目錄
File[] dirfiles = dir.listFiles(new FileFilter() {
// 自定義過濾規則 如果可以循環(包含子目錄) 或則是以.class結尾的檔案(編譯好的java類檔案)
@Override
public boolean accept(File file) {
return (recursive && file.isDirectory()) || (file.getName().endsWith(".class"));
}
});
// 循環所有檔案
for (File file : dirfiles) {
// 如果是目錄 則繼續掃描
if (file.isDirectory()) {
findClassesInPackageByFile(packageName + "." + file.getName(), file.getAbsolutePath(), recursive, classes);
} else {
// 如果是java類檔案 去掉後面的.class 隻留下類名
String className = file.getName().substring(0, file.getName().length() - 6);
try {
// 添加到集合中去
// classes.add(Class.forName(packageName + '.' +
// className));
classes.add(Thread.currentThread().getContextClassLoader().loadClass(packageName + '.' + className));
} catch (ClassNotFoundException e) {
// log.error("添加使用者自定義視圖類錯誤 找不到此類的.class檔案");
e.printStackTrace();
}
}
}
}
}
IOC DI具體實作邏輯
package cn.tedu.myspring;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class MyClassPathXMLApplicationContext {
private HashMap<String, Object> hashMap = new HashMap();
private Set<Class> set = new HashSet();
/**
* 對外提供一個代參的構造器,pakge為傳入的包名的路徑
* 将包内所有的位元組碼檔案擷取,篩選出包含自定義注解的位元組碼檔案放入Set集合中
* 為什麼要采用set集合呢?因為set集合中的元素具有不可重複性,保證位元組碼的唯一性
* 調用get方法将帶有表示的類執行個體化,并對注解的值進行判斷,如果注解有值,當做key存入map
* 如果沒有值首字母小寫當做key存入map
* get方法調用add()方法,實作對屬性的注入
*
* @param pakge
*/
public MyClassPathXMLApplicationContext(String pakge) {
Set<Class<?>> classes = MyTools.getClasses(pakge);//擷取所有的位元組碼檔案
Iterator<Class<?>> iterator = classes.iterator();//擷取set集合的疊代器準備将set集合中的位元組碼檔案疊代出來
while (iterator.hasNext()) {//開始疊代
Class<?> next = iterator.next();//擷取目前指針的元素,并将指針下移
if (next.isAnnotationPresent(CGB.class)) {//判斷目前位元組碼檔案TYPE屬性上是否有自己定義的CGB注解
set.add(next);//如果包含自定義的CGB注解調用set集合的add()方法,将位元組碼檔案放入set集合
}
}
try {
get();//調用get方法 将對象放入map集合中
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 将帶有表示的類執行個體化,并對注解的值進行判斷,如果注解有值,當做key存入map
* 如果沒有值首字母小寫當做key存入map,調用自定義的add()方法對屬性進行指派
*
* @throws Exception
*/
private void get() throws Exception {
Class next = null;
Object o = null;
Iterator<Class> iterator = set.iterator();//疊代包含自定義注解的位元組碼檔案
while (iterator.hasNext()) {
next = iterator.next();//擷取目前位元組碼檔案,并将指針下移
o = next.newInstance();//将位元組碼檔案執行個體化
CGB annotation = (CGB) next.getAnnotation(CGB.class);//類上的注解,并将注解強制轉換成自定義注解類型
String value = annotation.value();//擷取注解的value值
if (value.equals("")) {//對注解的值進行判斷,是否為空,為空則将類名小寫作為key放入map集合當中
String simpleName = next.getSimpleName();//通過目前類的位元組碼檔案擷取目前類名
String s = simpleName.substring(0, 1).toLowerCase() + simpleName.substring(1, simpleName.length());//将類名首字母變成小寫
hashMap.put(s, o);//放入map集合
} else {
hashMap.put(value, o);//如果目前注解的value不為空,直接存入map集合
}
}
add(next, o);//調用add()方法實作對包含指派注解的屬性的指派操作
}
/**
* 對屬性進行指派
*
* @param clazz 要指派的類的位元組碼檔案
* @param o 位元組碼執行個體化的對象
* @param <T>代表位元組碼類的類型
* @throws Exception
*/
private <T> void add(Class<T> clazz, Object o) throws Exception {
synchronized (MyClassPathXMLApplicationContext.class) {
Field[] declaredFields = clazz.getDeclaredFields();//通過暴力反射擷取所有的屬性
for (Field declaredField : declaredFields) {//對屬性進行周遊操作
if (declaredField.isAnnotationPresent(value.class)) {//如果目前屬性上包含自定義的value注解
value annotation = declaredField.getAnnotation(value.class);//擷取屬性上value注解的對象
String value = annotation.value();//擷取value注解的值
Class<?> type1 = declaredField.getType();//擷取目前屬性的類型的位元組碼檔案
String s1 = String.valueOf(type1);//将位元組碼檔案轉換成字元串
switch (s1) {//對s1進行比較
case "class java.lang.Integer"://如果是Integer類型
Integer a = Integer.valueOf(value);//将String字元串轉換成Integer類型
declaredField.setAccessible(true);//将私有屬性設定可見
declaredField.set((T) o, a);//通過暴力反射擷取屬性 以下類似不過多注釋
break;
case "class java.lang.String":
declaredField.setAccessible(true);
declaredField.set((T) o, value);
break;
case "class java.lang.Double":
Double b = Double.valueOf(value);
declaredField.setAccessible(true);
declaredField.set((T) o, b);
break;
case "class java.lang.Float":
Float c = Float.valueOf(value);
declaredField.setAccessible(true);
declaredField.set((T) o, c);
break;
case "class java.lang.Character":
if (value.length() == 1) {
declaredField.setAccessible(true);
declaredField.set((T) o, value.toCharArray()[0]);
} else {
throw new Exception();
}
break;
case "class java.lang.Byte":
Byte e = Byte.valueOf(value);
declaredField.setAccessible(true);
declaredField.set((T) o, e);
break;
case "class java.lang.Short":
Short f = Short.valueOf(value);
declaredField.setAccessible(true);
declaredField.set((T) o, f);
break;
case "class java.lang.Long":
Long fg = Long.valueOf(value);
declaredField.setAccessible(true);
declaredField.set((T) o, fg);
break;
case "class java.lang.Boolean":
Boolean aBoolean = Boolean.valueOf(value);
declaredField.setAccessible(true);
declaredField.set((T) o, aBoolean);
break;
default:
//如果類型為引用資料類型執行default裡面的内容
declaredField.setAccessible(true);//将私有屬性設定可見
if (value.equals("")) {//如果value注解的值為預設值執行此分支
String s = declaredField.getName().substring(0, 1).toLowerCase() + declaredField.getName().substring(1, declaredField.getName().length());
add(getBean(s).getClass(), getBean(s));//遞歸本方法将将引用資料類型中包含自定義注解value的屬性進行指派
declaredField.set((T) o, getBean(s));//對屬性進行指派
}else {
add(getBean(value).getClass(), getBean(value));//遞歸本方法将将引用資料類型中包含自定義注解value的屬性進行指派
declaredField.set((T) o, getBean(value));//對屬性進行指派
}
}
}
}
}
}
/**
* 提供的擷取對象的方法
*
* @param id
* @return
*/
public Object getBean(String id) {
return hashMap.get(id);
}
}
建立Pojo類用于作為封裝類
package cn.tedu.test;
import cn.tedu.myspring.CGB;
import cn.tedu.myspring.value;
@CGB
public class Pojo {
@value("張三")
private String name;
@value("13")
private Integer age;
@value("pojo1")
private Pojo1 pojo1;
@Override
public String toString() {
return "Pojo{" +
"name='" + name + '\'' +
", age=" + age +
", pojo1=" + pojo1 +
'}';
}
}
建立Pojo1 類用于作為封裝類
package cn.tedu.test;
import cn.tedu.myspring.CGB;
import cn.tedu.myspring.value;
@CGB
public class Pojo1 {
@value("中行收")
private String name;
@value("121")
private Integer age;
@value("pojo2")
private Pojo2 pojo2;
@Override
public String toString() {
return "Pojo1{" +
"name='" + name + '\'' +
", age=" + age +
", pojo2=" + pojo2 +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
建立Pojo2類用于封裝類
package cn.tedu.test;
import cn.tedu.myspring.CGB;
import cn.tedu.myspring.value;
@CGB
public class Pojo2 {
@value("李四")
private String name;
@value("99")
private Integer age;
@Override
public String toString() {
return "Pojo2{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
建立Test類測試資料
package cn.tedu.test;
import cn.tedu.myspring.CGB;
import cn.tedu.myspring.MyClassPathXMLApplicationContext;
@CGB
public class Test {
public static void main(String[] args) {
MyClassPathXMLApplicationContext m = new MyClassPathXMLApplicationContext("cn.tedu.test");
System.out.println(m.getBean("pojo"));
}
}
運作結果
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TP31keNRVT5NmaOBDOsJGcohVYsR2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLxMjN4UTMyIjM3EzNwEjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
相信你敲完之後會對注解反射以及Spring Ioc會有新的了解