一、配置文件方式
-------------------------------------配置文件方式---------------------------------------
pom.xml
4.0.0
com.xx
springTest
0.0.1-SNAPSHOT
5.2.8.RELEASE
org.springframework
spring-context
${spring.version}
创建实体类
package com.xx.po;
public class User {
private Integer id;
private String name;
public User(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
}
创建dao接口与实现类
package com.xx.dao;
import com.xx.po.User;
public interface UserDao {
public User getUser();
}
package com.xx.dao.impl;
import com.xx.dao.UserDao;
import com.xx.po.User;
public class UserDaoImpl implements UserDao{
@Override
public User getUser() {
return new User(1,"test");
}
}
创建service接口与实现类
package com.xx.service;
import com.xx.po.User;
public interface UserService {
public User getUser();
}
package com.xx.service.impl;
import com.xx.dao.UserDao;
import com.xx.po.User;
import com.xx.service.UserService;
public class UserServiceImpl implements UserService{
private UserDao userDao;
@Override
public User getUser() {
return userDao.getUser();
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
在类路径下创建spring.xml配置文件
Spring配置文件就相当于一个容器。此容器中负责创建对象,并实现对象与对象之间的装配。
java中每一个类都是一个bean。所以上面的bean标签,就是在容器中创建一个java对象。
bean标签中的class属性,就是类名; id属性,就是对象名。
property标签,是给bean的属性注入其它对象。name属性,就是对象属性名; ref属性,就是给属性注入的对象。(如果想要注入基本数据类型,那么使用value属性)
给bean的属性注入其它对象,默认使用 get/set 方法注入。也可以使用其它方式注入:构造方法注入、P命名空间注入等。
测试类
package com.xx;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xx.po.User;
import com.xx.service.UserService;
public class MySpringTest {
public static void main(String[] args) {
//读取Spring配置文件,获取Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
//通过Spring容器的getBean方法获得对象
UserService service = (UserService)context.getBean("userService");
User user = service.getUser();
System.out.println(user);
}
}
------------------------------------- 配置文件方式--------------------------------------
二、注解方式
-------------------------------------注解方式--------------------------------------------
pom.xml
4.0.0
com.xx
springTest
0.0.1-SNAPSHOT
5.2.8.RELEASE
org.springframework
spring-context
${spring.version}
创建实体类
package com.xx.po;
public class User {
private Integer id;
private String name;
public User(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
}
创建dao接口与实现类
package com.xx.dao;
import com.xx.po.User;
public interface UserDao {
public User getUser();
}
package com.xx.dao.impl;
import org.springframework.stereotype.Repository;
import com.xx.dao.UserDao;
import com.xx.po.User;
//@Component
@Repository //不起名字就默认类名首字母小写userDaoImpl
public class UserDaoImpl implements UserDao{
public User getUser() {
return new User(1,"test");
}
}
创建service接口与实现类
package com.xx.service;
import com.xx.po.User;
public interface UserService {
public User getUser();
}
package com.xx.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.xx.dao.UserDao;
import com.xx.po.User;
import com.xx.service.UserService;
//@Component("userService")
@Service("userService")
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
//注意:UserDao属性自动注入,所以就可以不用get/set方法了
public User getUser() {
return userDao.getUser();
}
}
在类路径下创建spring.xml配置文件
测试类
package com.xx;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xx.po.User;
import com.xx.service.UserService;
public class MySpringTest {
public static void main(String[] args) {
//读取Spring配置文件,获取Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
//通过Spring容器的getBean方法获得对象
UserService service = (UserService)context.getBean("userService");
User user = service.getUser();
System.out.println(user);
}
}
-------------------------------------注解方式--------------------------------------------
三、javaConfig方式
-------------------------------------javaConfig方式-------------------------------------
javaConfig,是在 Spring 3.0 开始从一个独立的项目并入到 Spring 中的。javaConfig 可以看成一个用于完成 Bean 装配的 Spring 配置文件,即 Spring 容器,只不过该容器不是 XML文件,而是由程序员使用 java 自己编写的 java 类。
一个类中只要标注了@Configuration注解,这个类就可以为spring容器提供Bean定义的信息了,或者说这个类就成为一个spring容器了。
标注了@Configuration和标注了@Component的类一样是一个Bean,可以被Spring的 context:component-scan 标签扫描到。类中的每个标注了@Bean的方法都相当于提供了一个Bean的定义信息。
pom.xml
4.0.0
com.xx
springTest
0.0.1-SNAPSHOT
5.2.8.RELEASE
org.springframework
spring-context
${spring.version}
创建实体类
package com.xx.po;
public class User {
private Integer id;
private String name;
public User(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
}
创建dao接口与实现类
package com.xx.dao;
import com.xx.po.User;
public interface UserDao {
public User getUser();
}
package com.xx.dao.impl;
import com.xx.dao.UserDao;
import com.xx.po.User;
public class UserDaoImpl implements UserDao{
@Override
public User getUser() {
return new User(1,"test");
}
}
创建service接口与实现类
package com.xx.service;
import com.xx.po.User;
public interface UserService {
public User getUser();
}
package com.xx.service.impl;
import com.xx.dao.UserDao;
import com.xx.po.User;
import com.xx.service.UserService;
public class UserServiceImpl implements UserService{
private UserDao userDao;
@Override
public User getUser() {
return userDao.getUser();
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
spring.xml
创建javaConfig类
package com.xx;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.xx.dao.UserDao;
import com.xx.dao.impl.UserDaoImpl;
import com.xx.service.UserService;
import com.xx.service.impl.UserServiceImpl;
@Configuration
public class AppConfig {
@Bean
public UserDao userDao() {
return new UserDaoImpl();
}
@Bean
public UserService userService() {
//这里不能声明接口类型
UserServiceImpl userService = new UserServiceImpl();
//配置依赖关系(需要set方法)
userService.setUserDao(userDao());
return userService;
}
}
测试
package com.xx;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xx.po.User;
import com.xx.service.UserService;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
UserService service = (UserService)context.getBean("userService");
User user = service.getUser();
System.out.println(user);
}
}
-------------------------------------javaConfig方式-------------------------------------------------