文章目录 1.基本使用 2.自己实现spring 注解 1.需求分析 2.思路分析图 3.编写自定义注解 ComponentScan 4.编写配置类SunSpringConfig 5.编写容器SunSpringApplicationContext 6.测试 3.自动装配 1.AutoWired方式 1.基本说明 2.UserDao.java 3.UserService.java 4.测试 2.Resource方式(推荐) 4.泛型依赖注入
1.基本使用
1.基本介绍
2.快速入门
1.引入jar包
java _14">2.MyComponent.java
java">package com. sxs. spring . component ; import org. spring framework. stereotype. Component ; @Component
public class MyComponent {
}
java _33">3.UserAction.java
java">package com. sxs. spring . component ; import org. spring framework. stereotype. Controller ;
@Controller
public class UserAction {
}
java">package com. sxs. spring . component ; import org. spring framework. stereotype. Repository ;
@Repository
public class UserDao {
}
java _69">4.UserService.java
java">package com. sxs. spring . component ; import org. spring framework. stereotype. Service ;
@Service
public class UserService {
}
bean s05xml_87">5.bean s05.xml
<?xml version="1.0" encoding="UTF-8"?>
< bean s xmlns = " http://www.spring framework.org/schema/bean s" xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance" xmlns: context= " http://www.spring framework.org/schema/context" xsi: schemaLocation= " http://www.spring framework.org/schema/bean s http://www.spring framework.org/schema/bean s/spring -bean s.xsd http://www.spring framework.org/schema/context https://www.spring framework.org/schema/context/spring -context.xsd" > < context: component-scan base-package = " com.sxs.spring .component" > </ context: component-scan>
</ bean s>
bean _103">6.断点查看bean 对象是否创建
7.测试
java"> @Test public void setBeanByAnnotation ( ) { ApplicationContext ioc = new ClassPathXmlApplicationContext ( "bean s05.xml" ) ; MyComponent component = ioc. getBean ( MyComponent . class ) ; UserAction action = ioc. getBean ( UserAction . class ) ; UserDao userDao = ioc. getBean ( UserDao . class ) ; UserService userService = ioc. getBean ( UserService . class ) ; System . out. println ( "" + component + action + userDao + userService) ; }
3.注意事项和细节
1.需求分析
2.思路分析图
3.编写自定义注解 ComponentScan
java">package com. sxs. spring . annotation ; 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 )
public @interface ComponentScan { String value ( ) default "" ;
}
4.编写配置类SunSpringConfig
java">package com. sxs. spring . annotation ;
@ComponentScan ( value = "com.sxs.spring .component" )
public class SunSpringConfig { }
5.编写容器SunSpringApplicationContext
java">package com. sxs. spring . annotation ; import org. spring framework. stereotype. Component ;
import org. spring framework. stereotype. Controller ;
import org. spring framework. stereotype. Repository ;
import org. spring framework. stereotype. Service ;
import org. spring framework. util. StringUtils ; import java . io. File ;
import java . net. URL ;
import java . util. concurrent. ConcurrentHashMap ;
public class SunSpringApplicationContext { private Class configClass; private final ConcurrentHashMap < String , Object > ioc = new ConcurrentHashMap < > ( ) ; public SunSpringApplicationContext ( Class configClass) throws ClassNotFoundException , InstantiationException , IllegalAccessException { this . configClass = configClass; ComponentScan componentScan = ( ComponentScan ) this . configClass. getDeclaredAnnotation ( ComponentScan . class ) ; String path = componentScan. value ( ) ; ClassLoader classLoader = SunSpringApplicationContext . class . getClassLoader ( ) ; path = path. replace ( "." , "/" ) ; URL resource = classLoader. getResource ( path) ; File file = new File ( resource. getFile ( ) ) ; if ( file. isDirectory ( ) ) { File [ ] files = file. listFiles ( ) ; for ( File f : files ) { String absolutePath = f. getAbsolutePath ( ) ; if ( absolutePath. endsWith ( ".class" ) ) { String className = absolutePath. substring ( absolutePath. lastIndexOf ( "\\" ) + 1 , absolutePath. indexOf ( "." ) ) ; String fullPath = path. replace ( "/" , "." ) + "." + className; Class < ? > aClass = classLoader. loadClass ( fullPath) ; if ( aClass. isAnnotationPresent ( Component . class ) || aClass. isAnnotationPresent ( Controller . class ) || aClass. isAnnotationPresent ( Service . class ) || aClass. isAnnotationPresent ( Repository . class ) ) { Class < ? > clazz = Class . forName ( fullPath) ; Object o = clazz. newInstance ( ) ; ioc. put ( StringUtils . uncapitalize ( className) , o) ; } } } System . out. println ( "ok" ) ; } } public Object getBean ( String name) { return ioc. get ( name) ; } }
6.测试
java">package com. sxs. spring . annotation ; import org. junit. jupiter. api. Test ;
public class test { @Test public void SunSpringApplicationContextTest ( ) throws ClassNotFoundException , InstantiationException , IllegalAccessException { SunSpringApplicationContext ioc = new SunSpringApplicationContext ( SunSpringConfig . class ) ; Object myComponent = ioc. getBean ( "myComponent" ) ; Object userAction = ioc. getBean ( "userAction" ) ; Object userDao = ioc. getBean ( "userDao" ) ; Object userService = ioc. getBean ( "userService" ) ; System . out. println ( "" + myComponent + userService + userAction + userDao) ; }
}
3.自动装配
1.AutoWired方式
1.基本说明
java">package com. sxs. spring . component ; import org. spring framework. stereotype. Repository ;
@Repository
public class UserDao { public void sayHi ( ) { System . out. println ( "hi" ) ; }
}
java _327">3.UserService.java
java">package com. sxs. spring . component ; import org. spring framework. bean s. factory. annotation. Autowired ;
import org. spring framework. stereotype. Service ;
@Service
public class UserService { @Autowired UserDao userDao; public void sayHi ( ) { userDao. sayHi ( ) ; }
}
4.测试
java"> @Test public void autowireTest ( ) { ApplicationContext ioc = new ClassPathXmlApplicationContext ( "bean s05.xml" ) ; UserService bean = ioc. getBean ( "userService" , UserService . class ) ; bean . sayHi ( ) ; }
2.Resource方式(推荐)
1.基本说明
java">package com. sxs. spring . component ; import org. spring framework. stereotype. Repository ;
@Repository
public class UserDao { public void sayHi ( ) { System . out. println ( "hi" ) ; }
}
java _397">3.UserService.java
java">package com. sxs. spring . component ; import org. spring framework. stereotype. Service ; import java x. annotation. Resource ;
@Service
public class UserService { @Resource UserDao userDao; public void sayHi ( ) { userDao. sayHi ( ) ; }
}
4.泛型依赖注入
1.基本说明
1.基本介绍
2.参考关系图