3、通过spring方式来调用sayhello方法。(spring di,ioc)
首先配置环境,和导入相对应的jar包
(commons-logging-1.1.1.jar、spring-aop-4.3.7.RELEASE.jar、
spring-aspects-4.3.7.RELEASE.jar、spring-beans-4.3.7.RELEASE.jar、
spring-context-4.3.7.RELEASE.jar、spring-core-4.3.7.RELEASE.jar、spring-expression-4.3.7.RELEASE.jar),
再配置相关的spring.xml文件(该文件内使用的接口在上一篇文章中)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<bean id="helloS" class="com.xiao.dao.SayHelloImp"></bean>
</beans>
最后测试
package com.xiao.test;
import com.xiao.dao.SayHello;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringJarTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("spring01.xml");
SayHello h = ac.getBean("helloS", SayHello.class);
h.hello("星星");
}
}
运行结果
4、自定义2个实体类。然后一个实体类作为另一个实体类的属性。实现里面的某个方法,得到输出属性的值。
- 实体类里要有无参和有参构造方法,以及set/get方法
- Address实体类
package com.xiao.bean;
public class Address {
private String homeAddress;
private String schoolAddress;
public Address() {
}
public String getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(String homeAddress) {
this.homeAddress = homeAddress;
}
public String getSchoolAddress() {
return schoolAddress;
}
public void setSchoolAddress(String schoolAddress) {
this.schoolAddress = schoolAddress;
}
public Address(String homeAddress, String schoolAddress) {
this.homeAddress = homeAddress;
this.schoolAddress = schoolAddress;
}
}
- Emp实体类
package com.xiao.bean;
public class Emp {
private int eid;
private String ename;
private String sex;
private Address address;
public Emp() {
}
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Emp(int eid, String ename, String sex, Address address) {
this.eid = eid;
this.ename = ename;
this.sex = sex;
this.address = address;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Emp{" +
"eid=" + eid +
", ename='" + ename + '\'' +
", sex='" + sex + '\'' +
", address=" + address +
'}';
}
}
- 配置spring.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<bean id="helloS" class="com.xiao.dao.SayHelloImp"></bean>
<bean id="address" class="com.xiao.bean.Address">
<property name="homeAddress" value="九城老街"></property>
<property name="schoolAddress" value="星光大道"></property>
</bean>
<bean id="emp" class="com.xiao.bean.Emp">
<property name="eid" value="1001"></property>
<property name="ename" value="大BOSS"></property>
<property name="sex" value="男"></property>
<property name="address" ref="address"></property>
</bean>
</beans>
- 测试
package com.xiao.test;
import com.xiao.bean.Emp;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringBeanTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("spring01.xml");
Emp emp = (Emp) ac.getBean("emp");
System.out.println(emp.getAddress().getHomeAddress());
}
}
- 运行结果