Test:
package com.m.ioc;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main (String[] args) {
ApplicationContext applicationContext= new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
userDao.say(3,3.14);
userDao.say(5,3.14);
}
}
Test01:
package com.m.ioc;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test01 {
public static void main(String[] args){
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService =
(UserService) applicationContext.getBean("userService");
userService.say(4,18.84);
}
}
UserDao.java:
package com.m.ioc;
public interface UserDao {
public void say(int ll,double paipai);
}
UserDaoimp.java:
package com.m.ioc;
public class UserDaoimp implements UserDao{
int l=4;double pai=3.14;
public void say(int ll,double paipai)
{
l=ll;pai=paipai;
System.out.println("底面周长是:"+(2*l*pai));
System.out.println("底面积是:"+(l*l*pai));
}
}
UserService.java:
package com.m.ioc;
public interface UserService {
public void say(int h,double c);
}
UserServiceimp.java:
package com.m.ioc;
public class UserServiceimp implements UserService{
double c=18.84;
private UserDao userDao;
public void setUserDao(UserDao userDao){
this.userDao = userDao;
}
public void say(int h,double c){
this.userDao.say(3,3.14);
System.out.println("圆柱体积是:"+(c*h));
}
}
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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<!-- 将指定类配置给Spring,让Spring创建其对象的实例 -->
<bean id="userDao" class="com.m.ioc.UserDaoimp" />
<bean id="userService" class="com.m.ioc.UserServiceimp" >
<property name="userDao" ref="userDao" />
</bean>
</beans>