spring入门(2)---第一个spring案例
程序员文章站
2024-03-18 16:30:46
...
直接上图:
源码:
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
推荐阅读
-
spring入门(2)---第一个spring案例
-
Spring Cloud系列(六) 改变Eureka server中注册服务的健康检测方式(Finchley.RC2版本)
-
spring-cloud Finchley 微服务架构从入门到精通【六】BUS RabbitMQ 配置刷新
-
疯子在思考之从零说MVC-2 博客分类: JAVA mvcstruts开源框架spring mvc3
-
现有web系统替换成Spring Boot2框架 之5 文件路径调整 spring bootmaven
-
现有web系统替换成Spring Boot2框架 之3 配置数据库驱动,事务控制 spring boot2数据库事务控制
-
JqGrid4.2实践-2-集成Spring MVC 博客分类: JS_JqGrid jqgridspringmvcjsonrest
-
Spring Roo 1.0.0.M2 发布
-
MongoDB最简单的入门教程之四:使用Spring Boot操作MongoDB 数据库mongoDBSpringBoot
-
【Spring】基础入门篇(二) 程序的耦合和解耦