工程的目录结构以及所需的jar包如下:
1.beans.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- The implicitly registered post-processors include AutowiredAnnotationBeanPostProcessor, CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor, as well as the aforementioned RequiredAnnotationBeanPostProcessor.--> <context:annotation-config/> <bean name="userDao1" class="com.bjsxt.dao.impl.UserDAOImpl"></bean> <bean name="userDao2" class="com.bjsxt.dao.impl.UserDAOImpl"></bean> <bean name="userService" class="com.bjsxt.service.UserService"></bean> </beans>
2.接口UserDao
package com.bjsxt.dao; import com.bjsxt.model.User; public interface UserDAO { public void save(User user); }
3.接口的实现UserDaoImpl
package com.bjsxt.dao.impl; import com.bjsxt.dao.UserDAO; import com.bjsxt.model.User; public class UserDAOImpl implements UserDAO { public void save(User user) { System.out.println("test--------"+user.getUsername()); System.out.println("user saved!"); } }
4.编写数据传输User类
package com.bjsxt.model; public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
5.UserService
package com.bjsxt.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import com.bjsxt.dao.UserDAO; import com.bjsxt.model.User; public class UserService { private UserDAO userDAO; public void add(User user) { userDAO.save(user); } public UserDAO getUserDAO() { return userDAO; } /*首先按名称来注入,由于没有名为"userDAO"的bean。所以采用类型来注入,但是com.bjsxt.dao.UserDAO类型的bean有2个,所以会报错。 *处理:搭配@Qualifier来使用,指定注入userDao1,userDao2中那个bean */ @Autowired @Qualifier("userDao2") //指定注入userDao2 public void setUserDAO(UserDAO userDAO) { this.userDAO = userDAO; } }
6.编写测试类UserServiceTest
package com.bjsxt.service; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bjsxt.model.User; public class UserServiceTest { @Test public void testAdd() { /**ApplicationContext是建立在BeanFactory之上的,BeanFactory它只是完成了Bean工厂的一些功能,像Bean的声明周期它都处理不了。而ApplicationContext除了能完成BeanFactory所有的功能之外,还能够完成 一些其他的附加的功能,比如说bean的声明的周期。 */ ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserService service = (UserService)ctx.getBean("userService"); User u = new User(); u.setUsername("zhangsan"); u.setPassword("gaoweigang"); service.add(u); } }
相关推荐
spring-security-helloworld-annotation
spring-boot-helloWorld:spring-boot的helloWorld版本 spring-boot-mybaits-annotation:注解版本 spring-boot-mybaits-xml:xml配置版本 spring-boot-mybatis-mulidatasource:springboot+mybatis多数据源最简解决...
spring-boot-helloWorld:spring-boot的helloWorld版本 spring-boot-mybaits-annotation:注解版本 spring-boot-mybaits-xml:xml配置版本 spring-boot-mybatis-mulidatasource:springboot+mybatis多数据源最简...
在本地运行该项目$ git clone https://github.com/mkyong/spring3-mvc-maven-annotation-hello-world$ mvn jetty:run 访问http://localhost:8080/spring3 ### 3。 将此项目导入Eclipse IDE $ mvn eclipse:eclipse ...
server','springboot-elasticsearch','springboot-freemarker','springboot-helloworld','springboot-mybatis','springboot-mybatis-annotationspringboot-mybatis-annotation','springboot-mybatis-mutil-...
- [spring-boot-helloWorld](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-helloWorld):Spring Boot 的 hello World 版本 - [spring-boot-web]...
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller;... return "Hello World!"; } }
Spring Boot 项目旨在... return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); } } 标签:Spring
是英文版本的哦,建议有耐心的读者下载. Available Pages · Documentation · Annotations · After Annotation · ...· XWork2 Hello World Tutorial · XWork Validation · XWork Value Stack
2.1.1 比Hello World更适用的实例 2.1.2 实例功能简介 2.2 环境准备 2.2.1 创建库表 2.2.2 建立工程 2.2.3 类包及Spring配置文件规划 2.3 持久层 2.3.1 建立领域对象 2.3.2 UserDao 2.3.3 LoginLogDao 2.3.4 在...
import org.springframework.web.bind.annotation.RestController; // @RestController返回的是json @RestController public class HelloWorldController { // http://localhost:8080/hello 返回的是文本"Hello ...
:spring-boot的helloWorld版本 :注解版本 :xml配置版本 spring-boot-mybatis-mulidatasource:springboot+mybatis多数据源最简解决方案 spring-boot-mybatis-annotation-mulidatasource:springboot+mybatis...
2.1.1 比Hello World更适用的实例 2.1.2 实例功能简介 2.2 环境准备 2.2.1 创建库表 2.2.2 建立工程 2.2.3 类包及Spring配置文件规划 2.3 持久层 2.3.1 建立领域对象 2.3.2 UserDao 2.3.3 LoginLogDao 2.3.4 在...
helloworld 一个最简单的Sping程序 autowired-list 演示多个具有相同接口的实现类型的Bean通过@Autowired 来注入列表变量中 constructor 演示如何将Bean通过构造函数的参数来注入对象中 import-annotation 演示如何...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
2.1.8 struts 2的运行流程分析—helloworld 48 2.1.9 struts 2的异常处理 52 2.2 struts 2配置精要 54 2.2.1 web.xml中struts 2的配置实现 54 2.2.2 struts 2属性配置文件struts.properties详解 55 2.2.3 ...
Buildfile: G:\eclipseEjb3\Ejb_HelloWorld_02\build.xmlprepare: [mkdir] Created dir: G:\eclipseEjb3\Ejb_HelloWorld_02\buildcompile: [javac] Compiling 3 source files t ... by caizhongda 2009-02-28 回复 ...
Java访问权限控制,为Java操作文件、写入文件分配合适的权限,定义写到文件的信息、定义文件,输出到c:/hello.txt、写信息到文件、关闭输出流。 Java绘制图片火焰效果 1个目标文件 摘要:Java源码,图形操作,火焰...
Java访问权限控制源代码 1个目标文件 摘要:Java源码,文件操作,权限控制 Java访问权限控制,为Java操作文件、写入文件分配合适的权限,定义写到文件的信息、定义文件,输出到c:/hello.txt、写信息到文件、关闭输出流...