Spring
Foo
package com
publicclass Foo {
private String name;
public String toStirng(){
return
}
Setget
}
package com
publicclass Bar {
private String add;
public String toStirng(){
return
}
Setget
}
package com
publicclass Main {
private Foo foo;
private Bar bar;
public String toString(){
return
}
Setget
}
<bean id=
<property name=
<property name=
</bean>
<bean id=
<property name=
</bean>
<bean id=
<property name=
</bean>
package com
import orgntext
import orgntext
ClassPathXmlApplicationContext;
publicclass Test {
publicstaticvoid main(String[] args) {
String[] locations = {
ApplicationContext ctx = new ClassPathXmlApplicationContext(locations);
Main main = (Main) ctx
system
}
}
Test
Main : [Foo Name is :Foo Bar Add is :Bar]
import org
publicclass Main {
@Autowired
private Foo foo;
@Autowired
private Bar bar;
public String toString(){
return
}
}
Spring
<!
<bean class=
AutowiredAnnotationBeanPostProcessor
<!— main Bean
<bean id=
<bean id=
<property name=
</bean>
<bean id=
<property name=
</bean>
publicclass Main {
@Autowired
@Qualifier(
private Foo foo;
@Autowired
private Bar bar;
public String toString(){
return
}
}
<bean class=
AutowiredAnnotationBeanPostProcessor
<bean id=
<bean id=
<property name=
</bean>
<bean id=
<property name=
</bean>
<bean id=
<property name=
</bean>
Test
Main : [Foo Name is :Foo
Springfoo
<context:annotation
<bean id=
<bean id=
<property name=
</bean>
<bean id=
<property name=
</bean>
<bean id=
<property name=
</bean>
<bean class=
AutowiredAnnotationBeanPostProcessor
<context:annotation
<context:annotationconfig/>
publicclass Foo {
private String name =
public String toStirng(){
return
}
}
@Component
@Component(
publicclass Main {
@Autowired
private Foo foo;
@Autowired
private Bar bar;
……
@Component <context:component
由於Spring MVC的Controller必須事先是一個Bean
代碼清單
@Controller //Controller
publicclass FooController {
@Autowired
private FooService fooService;
@RequestMapping(
public String[] list() {
String[] list = fooService
system
return list;
}
@RequestMapping(
publicvoid del(HttpServletRequest request
fooService
}
}
@Controller
@RequestMapping(
publicclass FooController {
@Autowired
private FooService fooService;
//listURL /doFoo
@RequestMapping(params =
public String[] list() {
String[] list = fooService
system
return list;
}
//delURL /doFoo
@RequestMapping(params =
publicvoid del(HttpServletRequest request
HttpServletResponse response) {
fooService
}
}
@Controller
@RequestMapping(
publicclass FooController {
//POST
@RequestMapping(params =
method = RequestMethod
public String submit(HttpServletRequest request
HttpServletResponse response){
system
return
}
}
submitPOSTURL
代碼清單
@Controller
@RequestMapping(
publicclass FooController {
@Autowired
private FooService fooService;
//delURL /doFoo
@RequestMapping(params =
public String del(int id) {
fooService
return
}
}
/doFoo
Spring del() id del() id del() String Spring ModelAndView Spring MVC
@Controller
@RequestMapping(
publicclass FooController {
@Autowired
private FooService fooService;
//delURL /doFoo
@RequestMapping(params =
public String del(@RequestParam(
fooService
return
}
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28654.html