xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd |
|
xml
version= "1.0" encoding ="UTF-8"?>
<
beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
>
<bean id="person" class="cn.itheima03.spring.di.annotation.Person" >bean>
<bean id="student" class="cn.itheima03.spring.di.annotation.Student" >bean>
<context:annotation-config> context:annotation-config>
beans
>
|
public
class Person {
private
Long pid;
// @Autowired//按照类型匹配
// @Qualifier("student")//id匹配
@Resource
(name=
"student")
private
Student student;
public
void show(){
this.student .show();
}
}
|
<property name= "student">
<ref bean= "student" />
property
>
|
xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd |
|
xml
version= "1.0" encoding ="UTF-8"?>
<
beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
>
<context:component-scan base-package="cn.itheima03.spring.scan" >context:component-scan>
beans
>
|
public
interface Document {
public
void read();
public
void write();
}
=========================
@Component
(
"excelDocument"
)
public
class ExcelDocument implements Document{
public
void read() {
System.
out.println("read excel" );
}
public
void write() {
System.
out.println("write excel" );
}
}
======================
@Component
(
"pdfDocument"
)
public
class PDFDocument implements Document{
public
void read() {
System.
out.println("read pdf" );
}
public
void write() {
System.
out.println("write pdf" );
}
}
=====================
@Component
(
"documentManager"
)
public
class
DocumentManager {
@Resource
(name=
"pdfDocument" )
private
Document
document
;
public
void
read(){
this .document .read();
}
public
void
write(){
this .document .write();
}
}
=======================
public
class DocumentTest {
@Test
public
void test(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml" );
DocumentManager documentManager = (DocumentManager) context.getBean(
"documentManager"
);
documentManager.read();
documentManager.write();
}
}
|
@Repository
(
"personDao"
)
public
class PersonDaoImpl implements PersonDao{
@Override
public
void savePerson() {
System.
out.println("save person" );
}
}
============================
@Service
(
"personService"
)
public
class PersonServiceImpl implements PersonService{
@Resource
(name=
"personDao")
private
PersonDao personDao;
public
void savePerson() {
this.personDao .savePerson();
}
}
============================
@Controller
(
"personAction"
)
public
class PersonActionImpl implements PersonAction{
@Resource
(name=
"personService")
private
PersonService personService;
public
void savePerson() {
this.personService .savePerson();
}
}
============================
public
class MVCTest {
@Test
public
void testMVC(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml" );
PersonAction personAction = (PersonAction)context.getBean(
"personAction"
);
personAction.savePerson();
}
}
|