Spring 内基于构造函数的依赖注入
Constructor-Based Dependency Injection within Spring
我有这个class:
@Service
public class DogUserService {
private final ManagerService managerService;
...
}
在我的 context.xml 上:
<bean id="managerService"
class="com.dogs.impl.services.ManagerService" />
但是当我 运行 应用程序时。我有这个错误:
rvice.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'com.dogs.impl.services.ManagerService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency a
nnotations: {}
您需要使用@Bean 注释向WebApplicationContext 提供bean。我已经在 @Configuration bean 中从 context.xml 创建了 managerService bean,并使用 @Bean 方法使该 bean 在 webApplicationContext 中可用。此 UserService class 具有作为自动装配的 managerService 具有此配置 bean 的 属性 @ConditionalOnBean。
下面是我是如何实现的。
申请class
@SpringBootApplication public class DemoApplication
{
public static void main(String[] args)
{
SpringApplication.run(DemoApplication.class, args);
}
}
用户服务
@Service
@ConditionalOnBean({EnableConfiguration.class})
public class UserService
{
@Autowired
private ManagerService managerService;
public String run() {
System.out.println("Going to Run from manager Service");
return managerService.run();
}
}
启用配置
@Configuration
public class EnableConfiguration implements InitializingBean
{
ManagerService managerService;
@Bean
public ManagerService mngrService() {
return managerService;
}
@Override public void afterPropertiesSet() throws Exception
{
ApplicationContext ctx = new ClassPathXmlApplicationContext( "context.xml" );
managerService = (ManagerService) ctx.getBean("managerService");
System.out.println("manager bean:" + managerService.toString());
mngrService();
}
}
ManagerService
public class ManagerService
{
private int a;
private int b;
public ManagerService(int a, int b) {
this.a = a;
this.b = b;
}
public String run() {
return this.toString();
}
@Override public String toString()
{
return "ManagerService{" + "a=" + a + ", b=" + b + '}';
}
}
服务控制器
@RestController
public class ServiceController
{
@Autowired
private UserService userService;
@GetMapping("/get")
public String test() {
return userService.run();
}
}
context.xml
<bean id="managerService" class="com.example.demo.ManagerService">
<constructor-arg index="0" value="2"/>
<constructor-arg index="1" value="5"/>
</bean>
我有这个class:
@Service
public class DogUserService {
private final ManagerService managerService;
...
}
在我的 context.xml 上:
<bean id="managerService"
class="com.dogs.impl.services.ManagerService" />
但是当我 运行 应用程序时。我有这个错误:
rvice.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'com.dogs.impl.services.ManagerService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency a
nnotations: {}
您需要使用@Bean 注释向WebApplicationContext 提供bean。我已经在 @Configuration bean 中从 context.xml 创建了 managerService bean,并使用 @Bean 方法使该 bean 在 webApplicationContext 中可用。此 UserService class 具有作为自动装配的 managerService 具有此配置 bean 的 属性 @ConditionalOnBean。
下面是我是如何实现的。
申请class
@SpringBootApplication public class DemoApplication
{
public static void main(String[] args)
{
SpringApplication.run(DemoApplication.class, args);
}
}
用户服务
@Service
@ConditionalOnBean({EnableConfiguration.class})
public class UserService
{
@Autowired
private ManagerService managerService;
public String run() {
System.out.println("Going to Run from manager Service");
return managerService.run();
}
}
启用配置
@Configuration
public class EnableConfiguration implements InitializingBean
{
ManagerService managerService;
@Bean
public ManagerService mngrService() {
return managerService;
}
@Override public void afterPropertiesSet() throws Exception
{
ApplicationContext ctx = new ClassPathXmlApplicationContext( "context.xml" );
managerService = (ManagerService) ctx.getBean("managerService");
System.out.println("manager bean:" + managerService.toString());
mngrService();
}
}
ManagerService
public class ManagerService
{
private int a;
private int b;
public ManagerService(int a, int b) {
this.a = a;
this.b = b;
}
public String run() {
return this.toString();
}
@Override public String toString()
{
return "ManagerService{" + "a=" + a + ", b=" + b + '}';
}
}
服务控制器
@RestController
public class ServiceController
{
@Autowired
private UserService userService;
@GetMapping("/get")
public String test() {
return userService.run();
}
}
context.xml
<bean id="managerService" class="com.example.demo.ManagerService">
<constructor-arg index="0" value="2"/>
<constructor-arg index="1" value="5"/>
</bean>