文章目录
- 1.基本使用
- 1.基本介绍
- 2.快速入门
- 1.引入jar包
- 2.MyComponent.java
- 3.UserAction.java
- 3.UserDao.java
- 4.UserService.java
- 5.beans05.xml
- 6.断点查看bean对象是否创建
- 7.测试
- 3.注意事项和细节
- 2.自己实现spring注解
- 1.需求分析
- 2.思路分析图
- 3.编写自定义注解ComponentScan
- 4.编写配置类SunSpringConfig
- 5.编写容器SunSpringApplicationContext
- 6.测试
- 3.自动装配
- 1.AutoWired方式
- 1.基本说明
- 2.UserDao.java
- 3.UserService.java
- 4.测试
- 2.Resource方式(推荐)
- 1.基本说明
- 2.UserDao.java
- 3.UserService.java
- 4.泛型依赖注入
-
1.基本使用
1.基本介绍
2.快速入门
1.引入jar包
2.MyComponent.java
package com.sxs.spring.component;
import org.springframework.stereotype.Component;
@Component
public class MyComponent {
}
3.UserAction.java
package com.sxs.spring.component;
import org.springframework.stereotype.Controller;
@Controller
public class UserAction {
}
3.UserDao.java
package com.sxs.spring.component;
import org.springframework.stereotype.Repository;
@Repository
public class UserDao {
}
4.UserService.java
package com.sxs.spring.component;
import org.springframework.stereotype.Service;
@Service
public class UserService {
}
5.beans05.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.sxs.spring.component"></context:component-scan>
</beans>
6.断点查看bean对象是否创建
7.测试
@Test
public void setBeanByAnnotation() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans05.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.注意事项和细节
2.自己实现spring注解
1.需求分析
2.思路分析图
3.编写自定义注解ComponentScan
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
package com.sxs.spring.annotation;
@ComponentScan(value = "com.sxs.spring.component")
public class SunSpringConfig {
}
5.编写容器SunSpringApplicationContext
package com.sxs.spring.annotation;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.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.测试
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.基本说明
2.UserDao.java
package com.sxs.spring.component;
import org.springframework.stereotype.Repository;
@Repository
public class UserDao {
public void sayHi() {
System.out.println("hi");
}
}
3.UserService.java
package com.sxs.spring.component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
UserDao userDao;
public void sayHi() {
userDao.sayHi();
}
}
4.测试
@Test
public void autowireTest() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans05.xml");
UserService bean = ioc.getBean("userService", UserService.class);
bean.sayHi();
}
2.Resource方式(推荐)
1.基本说明
2.UserDao.java
package com.sxs.spring.component;
import org.springframework.stereotype.Repository;
@Repository
public class UserDao {
public void sayHi() {
System.out.println("hi");
}
}
3.UserService.java
package com.sxs.spring.component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class UserService {
@Resource
UserDao userDao;
public void sayHi() {
userDao.sayHi();
}
}
4.泛型依赖注入
1.基本说明
1.基本介绍
2.参考关系图