Bla*_*man 481 java spring spring-mvc ioc-container autowired
关于控制(IoC
)的反转是如何工作的,我有点困惑Spring
.
假设我有一个名为的UserServiceImpl
实现UserService
接口的服务类.
怎么会这样@Autowired
?
而在我Controllers
,我怎么会instantiate
在instance
这个服务的?
我会做以下吗?
UserService userService = new UserServiceImpl();
Run Code Online (Sandbox Code Playgroud)
Boz*_*zho 676
首先,也是最重要的 - 所有Spring bean都被管理 - 它们"存活"在容器内,称为"应用程序上下文".
其次,每个应用程序都有一个入口点.Web应用程序有一个Servlet,JSF使用el-resolver等.此外,还有一个应用程序上下文被引导的地方和所有bean - 自动装配.在Web应用程序中,这可以是启动侦听器.
通过将一个bean的实例放入另一个bean的实例中的所需字段来实现自动装配.这两个类都应该是bean,即它们应该被定义为存在于应用程序上下文中.
应用程序上下文中的"生活"是什么?这意味着上下文实例化对象,而不是您.即 - 你永远不会new UserServiceImpl()
- 容器找到每个注入点并在那里设置一个实例.
在您的控制器中,您只需拥有以下内容:
@Controller // Defines that this class is a spring bean
@RequestMapping("/users")
public class SomeController {
// Tells the application context to inject an instance of UserService here
@Autowired
private UserService userService;
@RequestMapping("/login")
public void login(@RequestParam("username") String username,
@RequestParam("password") String password) {
// The UserServiceImpl is already injected and you can use it
userService.login(username, password);
}
}
Run Code Online (Sandbox Code Playgroud)
几点说明:
applicationContext.xml
,你应该让<context:component-scan>
这样的类被扫描了@Controller
,@Service
等注释.UserServiceImpl
也应该定义为bean - 使用<bean id=".." class="..">
或使用@Service
注释.由于它将是唯一的实现者UserService
,它将被注入.@Autowired
注释之外,Spring还可以使用XML可配置的自动装配.在这种情况下,所有具有与现有bean匹配的名称或类型的字段都会自动获取注入的bean.实际上,这是自动装配的最初想法 - 在没有任何配置的情况下为字段注入依赖关系.像其他的注解@Inject
,@Resource
也可以使用.Ben*_*n J 64
取决于您是使用注释路由还是bean XML定义路由.
假设你在你的bean中定义了bean applicationContext.xml
:
<beans ...>
<bean id="userService" class="com.foo.UserServiceImpl"/>
<bean id="fooController" class="com.foo.FooController"/>
</beans>
Run Code Online (Sandbox Code Playgroud)
应用程序启动时会发生自动装配.因此,fooController
为了论证而想要使用UserServiceImpl
该类,你需要注释如下:
public class FooController {
// You could also annotate the setUserService method instead of this
@Autowired
private UserService userService;
// rest of class goes here
}
Run Code Online (Sandbox Code Playgroud)
当它看到时@Autowired
,Spring将查找与applicationContext中的属性匹配的类,并自动注入它.如果您有多个UserService bean,那么您必须确定它应该使用哪个.
如果您执行以下操作:
UserService service = new UserServiceImpl();
Run Code Online (Sandbox Code Playgroud)
除非你自己设置,否则它不会接受@Autowired.
小智 19
@Autowired
是Spring 2.5中引入的注释,它仅用于注入.
例如:
class A {
private int id;
// With setter and getter method
}
class B {
private String name;
@Autowired // Here we are injecting instance of Class A into class B so that you can use 'a' for accessing A's instance variables and methods.
A a;
// With setter and getter method
public void showDetail() {
System.out.println("Value of id form A class" + a.getId(););
}
}
Run Code Online (Sandbox Code Playgroud)
@Autowired如何在内部工作?
前 -
class EnglishGreeting {
private Greeting greeting;
//setter and getter
}
class Greeting {
private String message;
//setter and getter
}
Run Code Online (Sandbox Code Playgroud)
.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>
Run Code Online (Sandbox Code Playgroud)
如果您正在使用@Autowired
class EnglishGreeting {
@Autowired //so automatically based on the name it will identify the bean and inject.
private Greeting greeting;
//setter and getter
}
Run Code Online (Sandbox Code Playgroud)
.xml文件如果不使用@Autowired它看起来很相似
<bean id="englishGreeting" class="com.bean.EnglishGreeting"></bean>
<bean id="greeting" class="com.bean.Greeting">
<property name="message" value="Hello World"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
如果仍有疑问,请通过以下现场演示
小智 6
您只需要使用注释来注释您的服务类UserServiceImpl
:
@Service("userService")
Run Code Online (Sandbox Code Playgroud)
Spring容器在注册为服务时将照顾此类的生命周期。
然后,您可以在控制器中自动对其进行连线(实例化)并使用其功能:
@Autowired
UserService userService;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
334200 次 |
最近记录: |