对于Spring中的控制反转(IoC)是如何工作的,我有点困惑。
假设我有一个名为UserServiceImpl的服务类,它实现了UserService接口。
@Autowired会怎么做?
在我的控制器中,我如何实例化这个服务的实例?
我能做下面的事情吗?
UserService userService = new UserServiceImpl();
对于Spring中的控制反转(IoC)是如何工作的,我有点困惑。
假设我有一个名为UserServiceImpl的服务类,它实现了UserService接口。
@Autowired会怎么做?
在我的控制器中,我如何实例化这个服务的实例?
我能做下面的事情吗?
UserService userService = new UserServiceImpl();
当前回答
简单地说,自动连接链接,现在的问题是谁做这个,哪种连接。 答案是:容器是这样做的,次要类型的连接是支持的,原语需要手动完成。
问:容器如何知道哪种类型的布线?
答:我们将其定义为byType,byName,构造函数。
问:有没有办法我们不定义类型的自动装配?
答:是的,它通过一个注释@Autowired来实现。
问:但是系统怎么知道,我需要选择这类辅助数据呢?
答:您将在spring.xml文件中提供该数据,或者通过对类使用原型注释,以便容器自己为您创建对象。
其他回答
请记住,您必须通过在spring配置文件中添加元素<context:annotation-config/>来启用@Autowired注释。这将注册AutowiredAnnotationBeanPostProcessor,它负责处理注释。
然后,您可以使用字段注入方法自动装配您的服务。
public class YourController{
@Autowired
private UserService userService;
}
我从Spring @autowired注释中找到了这个
标准方法:
@RestController
public class Main {
UserService userService;
public Main(){
userService = new UserServiceImpl();
}
@GetMapping("/")
public String index(){
return userService.print("Example test");
}
}
用户业务接口:
public interface UserService {
String print(String text);
}
UserServiceImpl经济舱:
public class UserServiceImpl implements UserService {
@Override
public String print(String text) {
return text + " UserServiceImpl";
}
}
输出:示例test UserServiceImpl
这是一个很好的紧密耦合类的例子,糟糕的设计示例,并且在测试时会出现问题(PowerMockito也很糟糕)。
现在让我们来看看SpringBoot依赖注入,一个松耦合的好例子:
界面保持不变,
主要课程:
@RestController
public class Main {
UserService userService;
@Autowired
public Main(UserService userService){
this.userService = userService;
}
@GetMapping("/")
public String index(){
return userService.print("Example test");
}
}
ServiceUserImpl经济舱:
@Component
public class UserServiceImpl implements UserService {
@Override
public String print(String text) {
return text + " UserServiceImpl";
}
}
输出:示例test UserServiceImpl
现在写测试就很简单了:
@RunWith(MockitoJUnitRunner.class)
public class MainTest {
@Mock
UserService userService;
@Test
public void indexTest() {
when(userService.print("Example test")).thenReturn("Example test UserServiceImpl");
String result = new Main(userService).index();
assertEquals(result, "Example test UserServiceImpl");
}
}
我在构造函数上展示了@Autowired注释,但它也可以用于setter或field。
Spring依赖注入帮助您从类中移除耦合。 而不是像这样创建对象:
UserService userService = new UserServiceImpl();
你将在引入DI之后使用它:
@Autowired
private UserService userService;
为了实现这一点,您需要在ServiceConfiguration文件中创建服务的bean。之后,你需要导入ServiceConfiguration类到你的WebApplicationConfiguration类,这样你就可以像这样将bean自动装配到你的控制器中:
public class AccController {
@Autowired
private UserService userService;
}
您可以在这里找到一个基于POC的java配置 的例子。
简单地说,自动连接链接,现在的问题是谁做这个,哪种连接。 答案是:容器是这样做的,次要类型的连接是支持的,原语需要手动完成。
问:容器如何知道哪种类型的布线?
答:我们将其定义为byType,byName,构造函数。
问:有没有办法我们不定义类型的自动装配?
答:是的,它通过一个注释@Autowired来实现。
问:但是系统怎么知道,我需要选择这类辅助数据呢?
答:您将在spring.xml文件中提供该数据,或者通过对类使用原型注释,以便容器自己为您创建对象。
@Autowired内部是如何工作的?
例子:
class EnglishGreeting {
private Greeting greeting;
//setter and getter
}
class Greeting {
private String message;
//setter and getter
}
.xml文件,如果不使用@Autowired将看起来类似:
<bean id="englishGreeting" class="com.bean.EnglishGreeting">
<property name="greeting" ref="greeting"/>
</bean>
<bean id="greeting" class="com.bean.Greeting">
<property name="message" value="Hello World"/>
</bean>
如果你使用@Autowired,那么:
class EnglishGreeting {
@Autowired //so automatically based on the name it will identify the bean and inject.
private Greeting greeting;
//setter and getter
}
.xml文件,如果不使用@Autowired将看起来类似:
<bean id="englishGreeting" class="com.bean.EnglishGreeting"></bean>
<bean id="greeting" class="com.bean.Greeting">
<property name="message" value="Hello World"/>
</bean>
如果仍有疑问,请通过下面的现场演示
@Autowired内部是如何工作的?