spring入门(2)---第一个spring案例

直接上图:

源码:

HelloDao.java

package www.csdn.spring.dao;

public interface HelloDao {
 public void sayHello();
}


HelloDAoImpl.java

package www.csdn.spring.dao;

public class HelloDaoImpl implements HelloDao{
	public HelloDaoImpl() {
		System.out.println("HelloDaoImpl实例化.......");
	}
	@Override
	public void sayHello() {
		System.out.println("say:hello");
	}

}


DemoTest.java

package www.csdn.spring.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import www.csdn.spring.dao.HelloDao;
import www.csdn.spring.dao.HelloDaoImpl;
import www.csdn.spring.service.HelloService;

public class DemoTest {
	@Test
	public void test(){
		HelloDao helloDao = new HelloDaoImpl();
		helloDao.sayHello();
		/*// 容器创建 实例化容器
		
				// 读取 classes 路径下面的文件  参数 动态参数、单个参数、数组 等
				ApplicationContext context = new ClassPathXmlApplicationContext(
						"spring.xml");
				
			//	HelloDao helloDao = (HelloDao) context.getBean("helloDaoImpl");
			//HelloDao helloDao =context.getBean("helloDaoImpl", HelloDao.class);
				
				HelloDaoImpl helloDaoImpl =context.getBean("helloDaoImpl", HelloDaoImpl.class);
				   
				
				//helloDao.sayHello();
				
				//helloSerivceImpl
				HelloService helloService = context.getBean("helloServiceImpl", HelloService.class);
			
			    helloService.sayHello();*/
		
	}
}	


spring-dao.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">


   <!-- spring容器 就是负责创建、管理、维护Bean 并且能够依赖注入到相应组件上 -->
   <bean id="helloDaoImpl" class="www.csdn.spring.dao.HelloDaoImpl" scope="singleton" lazy-init="default"></bean>
</beans>


spring-service.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">

   <bean id="helloServiceImpl" class="www.csdn.spring.service.HelloServiceImpl" scope="singleton" lazy-init="false">
      <property name="helloDao" ref="helloDaoImpl"/>
   </bean>
</beans>


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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
      

   <import resource="spring-dao.xml"/>
   <import resource="spring-service.xml"/>
   
</beans>


执行测试类之后控制台输出

HelloDaoImpl实例化.......
say:hello

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值