`
weigang.gao
  • 浏览: 468324 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

Spring HelloWorld annotation

 
阅读更多

工程的目录结构以及所需的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);
	}

}

 

  • 大小: 18.9 KB
分享到:
评论

相关推荐

    spring-security-helloworld-annotation

    spring-security-helloworld-annotation

    Spring boot 示例 官方 Demo

    spring-boot-helloWorld:spring-boot的helloWorld版本 spring-boot-mybaits-annotation:注解版本 spring-boot-mybaits-xml:xml配置版本 spring-boot-mybatis-mulidatasource:springboot+mybatis多数据源最简解决...

    Spring Boot Examples

    spring-boot-helloWorld:spring-boot的helloWorld版本 spring-boot-mybaits-annotation:注解版本 spring-boot-mybaits-xml:xml配置版本 spring-boot-mybatis-mulidatasource:springboot+mybatis多数据源最简...

    spring-3-mvc-hello-world-example-annotation

    在本地运行该项目$ 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 ...

    spring-boot很多实例

    server','springboot-elasticsearch','springboot-freemarker','springboot-helloworld','springboot-mybatis','springboot-mybatis-annotationspringboot-mybatis-annotation','springboot-mybatis-mutil-...

    spring-boot2.0全新教程实例20例.zip

    - [spring-boot-helloWorld](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-helloWorld):Spring Boot 的 hello World 版本 - [spring-boot-web]...

    SpringBootTestApplication

    import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller;... return "Hello World!"; } }

    Pivotal团队提供的微框架Spring Boot.zip

    Spring Boot 项目旨在... return "Hello World!";  }  public static void main(String[] args) throws Exception {  SpringApplication.run(Example.class, args);  } } 标签:Spring

    xworkdocs 对struts2学习有帮助

    是英文版本的哦,建议有耐心的读者下载. Available Pages · Documentation · Annotations · After Annotation · ...· XWork2 Hello World Tutorial · XWork Validation · XWork Value Stack

    Spring.3.x企业应用开发实战(完整版).part2

    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 在...

    springboot 基础简易实例, maven项目

    import org.springframework.web.bind.annotation.RestController; // @RestController返回的是json @RestController public class HelloWorldController { // http://localhost:8080/hello 返回的是文本"Hello ...

    spring-boot-examples

    :spring-boot的helloWorld版本 :注解版本 :xml配置版本 spring-boot-mybatis-mulidatasource:springboot+mybatis多数据源最简解决方案 spring-boot-mybatis-annotation-mulidatasource:springboot+mybatis...

    Spring3.x企业应用开发实战(完整版) part1

    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 在...

    spring-examples

    helloworld 一个最简单的Sping程序 autowired-list 演示多个具有相同接口的实现类型的Bean通过@Autowired 来注入列表变量中 constructor 演示如何将Bean通过构造函数的参数来注入对象中 import-annotation 演示如何...

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    spring-framework-reference4.1.4

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    低清版 大型门户网站是这样炼成的.pdf

    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 ...

    iuhyiuhkjh908u0980

    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访问权限控制,为Java操作文件、写入文件分配合适的权限,定义写到文件的信息、定义文件,输出到c:/hello.txt、写信息到文件、关闭输出流。 Java绘制图片火焰效果 1个目标文件 摘要:Java源码,图形操作,火焰...

    JAVA上百实例源码以及开源项目源代码

    Java访问权限控制源代码 1个目标文件 摘要:Java源码,文件操作,权限控制 Java访问权限控制,为Java操作文件、写入文件分配合适的权限,定义写到文件的信息、定义文件,输出到c:/hello.txt、写信息到文件、关闭输出流...

Global site tag (gtag.js) - Google Analytics