不使用依赖注入
TestController
package com.duohoob.spring.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.duohoob.spring.service.TestService;
import com.duohoob.spring.service.TestServiceImpl;
@RestController
public class TestController {
private TestService testService = new TestServiceImpl();
@RequestMapping("/test")
public String test() {
String resp = testService.test();
return resp;
}
}
TestService
package com.duohoob.spring.service;
public interface TestService {
String test();
}
TestServiceImpl
package com.duohoob.spring.service;
import com.duohoob.spring.dao.TestDao;
import com.duohoob.spring.dao.TestDaoImpl;
public class TestServiceImpl implements TestService {
private TestDao testDao = new TestDaoImpl();
@Override
public String test() {
String result = testDao.test();
return result;
}
}
TestDao
package com.duohoob.spring.dao;
public interface TestDao {
String test();
}
TestDaoImpl
package com.duohoob.spring.dao;
public class TestDaoImpl implements TestDao {
@Override
public String test() {
return "success";
}
}
在实例化service对象时,new关键字将TestController和TestServiceImpl耦合在了一起, 而实际上按照面向接口开发原则,controller并不需要关心TestService的具体实现。
如果不使用new的话,我们还有其它方式实例化service对象吗?
可以通过反射的方式实例化对象, 例如, Class.forName(“com.duohoob.spring.service.TestServiceImpl”).newInstance() 需要指定类的qualified name,其实还是耦合在了一起。
我们可以将类的qualified name配置在xml中,相当于是在xml中维护, 然后使用工厂模式通过反射生成对象, 关于工厂模式的描述,可以看:设计模式-工厂模式。
自定义ioc
pom添加maven依赖
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
</dependency>
beans.xml
我们在/src/main/resources下边创建一个 beans.xml,通常情况下,我们习惯叫它做application-context.xml,
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="testDao" class="com.duohoob.spring.dao.TestDaoImpl"/>
<bean id="testService" class="com.duohoob.spring.service.TestServiceImpl">
<property name="testDao" ref="testDao"/>
</bean>
</beans>
BeanFactory
package com.duohoob.spring.factory;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class BeanFactory {
private static Map<String, Object> map = new HashMap<String, Object>();
static {
InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("beans.xml");
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(in);
Element beans = document.getRootElement();
List<Node> beanList = beans.selectNodes("//bean");
for (Node node : beanList) {
Element element = (Element) node;
String id = element.attributeValue("id");
String clazz = element.attributeValue("class");
Object newInstance = Class.forName(clazz).newInstance();
map.put(id, newInstance);
}
List<Node> propertyList = beans.selectNodes("//property");
for (Node node : propertyList) {
Element element = (Element) node;
String name = element.attributeValue("name");
String ref = element.attributeValue("ref");
Element parent = element.getParent();
String id = parent.attributeValue("id");
Object parentObj = map.get(id);
Method[] methods = parentObj.getClass().getMethods();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
if (method.getName().equalsIgnoreCase("set" + name)) {
method.invoke(parentObj, map.get(ref));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static Object getBean(String id) {
return map.get(id);
}
}
改造TestController
package com.duohoob.spring.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.duohoob.spring.factory.BeanFactory;
import com.duohoob.spring.service.TestService;
@RestController
public class TestController {
private TestService testService = (TestService) BeanFactory.getBean("testService");
@RequestMapping("/test")
public String test() {
String resp = testService.test();
return resp;
}
}
改造TestServiceImpl
package com.duohoob.spring.service;
import com.duohoob.spring.dao.TestDao;
import com.duohoob.spring.factory.BeanFactory;
public class TestServiceImpl implements TestService {
private TestDao testDao;
public void setTestDao(TestDao testDao) {
this.testDao = testDao;
}
@Override
public String test() {
String result = testDao.test();
return result;
}
}
完整的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.12</version>
<relativePath/>
</parent>
<groupId>com.duohoob</groupId>
<artifactId>spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring</name>
<description>for spring</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
|