Java Spring Tutorial -- autowire自动装配
2013-12-08 11:21
330 查看
文件结构:
package com.zxl.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zxl.aop.service.ShapeService;
public class AppMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml");
//ShapeService shapeService=(ShapeService)ctx.getBean("shapeService");
ShapeService shapeService=ctx.getBean("shapeService",ShapeService.class);
System.out.println(shapeService.getCircle().getName());
}
}
package com.zxl.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zxl.aop.service.ShapeService;
public class AppMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml");
//ShapeService shapeService=(ShapeService)ctx.getBean("shapeService");
ShapeService shapeService=ctx.getBean("shapeService",ShapeService.class);
System.out.println(shapeService.getCircle().getName());
}
}
package com.zxl.aop.model; public class Circle { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.zxl.aop.model; public class Triangle { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.zxl.aop.service; import com.zxl.aop.model.Circle; import com.zxl.aop.model.Triangle; public class ShapeService { private Circle circle; private Triangle triangle; public Circle getCircle() { return circle; } public void setCircle(Circle circle) { this.circle = circle; } public Triangle getTriangle() { return triangle; } public void setTriangle(Triangle triangle) { this.triangle = triangle; } }
<?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean name="triangle" class="com.zxl.aop.model.Triangle"> <property name="name" value="triangle name"/> </bean> <bean name="circle" class="com.zxl.aop.model.Circle"> <property name="name" value="Circle name"/> </bean> <!-- autowire自动装配 --> <bean name="shapeService" class="com.zxl.aop.service.ShapeService" autowire="byName" /> </beans>
相关文章推荐
- Eclipse the user operation is waiting for "buiding workspace" to complete
- java 通过thrift-0.9.1读取hbase表数据
- hdu 1089 java
- ubuntu下eclipse 安装记录
- java集合类(四)About Set
- Serializable java序列化
- [Struts2] Action Implements SessionAware
- Java基础——String(图解String的不变性)
- 解析Unicode编码和Java char 类型
- Java项目经验
- SpringMvc在拦截其中获得rest风格的变量
- JavaMail教程
- Java 三大主流框架概述
- [Java] Thread-01 sleep-join-yield
- [Java] Thread-01 sleep-join-yield
- Java 如何连接 SQL 2008 R2
- java回文数
- override equals in Java
- struts2处理请求的过程分析
- 第十二天【java虐我千百遍,我待java如初恋】