Spring配置过程(一)反转控制/依赖注入IOC

一  配置过程

1. 创建一个项目


2. 导包
    Spring的包
    commons-logging(日志)

    jstl

   Junit(可不加,用于单元测试)

    
3.编写Dao层和Service层

4. 创建配置文件 beans.xml   (ApplicationContext.xml)
    beans的节点
        bean节点
            id
            class
            

5. 测试代码

二  示例

配置文件实现

1.创建项目SpringDemo2

2.导入需要的包

3.创建实体类User

package com.hp.pojo;

public class User {
	private int id;
	private String username;
	private String password;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
	}
}

创建Dao层接口IUserDao

package com.hp.dao;

public interface IUserDao {
	int login(String username,String password);
}

创建Dao层实现类UserDaoImpl.java

package com.hp.dao.impl;

import com.hp.dao.IUserDao;

public class UserDaoImpl implements IUserDao {

	@Override
	public int login(String username, String password) {
		if("admin".equals(username) && "123".equals(password)){
			return 1;
		}
		return 0;
	}

}

创建Service层接口IUserService

package com.hp.service;

public interface IUserService {
	int login(String username,String password);
}

创建Service层实现类UserServiceImpl.java

package com.hp.service.impl;

import com.hp.dao.IUserDao;
import com.hp.service.IUserService;

public class UserServiceImpl implements IUserService {
	private IUserDao iUserDao;
	
	public void setiUserDao(IUserDao iUserDao) {
		this.iUserDao = iUserDao;
	}

	@Override
	public int login(String username, String password) {
		// TODO Auto-generated method stub
		return iUserDao.login(username, password);
	}
}

4.创建配置文件ApplicationContext.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:tx="http://www.springframework.org/schema/tx"
       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/tx  http://www.springframework.org/schema/tx/spring-tx.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
			
		<bean id="IUserDao" class="com.hp.dao.impl.UserDaoImpl" />
		
		<bean id="IUserService" class="com.hp.service.impl.UserServiceImpl">
		<!-- Set注入 -->
			<property name="iUserDao"  ref="IUserDao"></property>			
		</bean>
</beans>

5.创建测试类MainTest

package com.hp.test;

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

import com.hp.dao.IUserDao;
import com.hp.dao.impl.UserDaoImpl;
import com.hp.service.IUserService;
import com.hp.service.impl.UserServiceImpl;

public class MainTest {
	/*
	 * 原始方法
	 */
	@Test
	public void login(){
		String username="admin";
		String password="123";
		
		IUserDao iUserDao=new UserDaoImpl();
		IUserService iUserService=new UserServiceImpl();
		int res = iUserService.login(username, password);
		if(res==1){
		     System.out.println("登录成功");
		}
		else{
	             System.out.println("登录失败");
		}
	}
	
	/*
	 * 松耦合
	 */
	@Test
	public void login1(){
		String username="admin";
		String password="123";
		ClassPathXmlApplicationContext context=
				new ClassPathXmlApplicationContext("ApplicationContext.xml");
		IUserService iUserService=(IUserService) context.getBean("IUserService");
		int res = iUserService.login(username, password);
		if(res==1){
		      System.out.println("登录成功");
		}
		else{
	              System.out.println("登录失败");
		}
	}
}

6.项目结构:



注解实现

1.创建项目SpringDemo3

2.导入需要的包(同上)

3.创建实体类User(同上)
创建Dao层接口IUserDao

package com.hp.dao;

public interface IUserDao {
	int login(String username,String password);
}
创建Dao层实现类UserDaoImpl.java

package com.hp.dao.impl;

import org.springframework.stereotype.Repository;

import com.hp.dao.IUserDao;

@Repository("IUserDao")
public class UserDaoImpl implements IUserDao {

	@Override
	public int login(String username, String password) {
		if("admin".equals(username) && "123".equals(password)){
			return 1;
		}
		return 0;
	}

}
创建Service层接口IUserService

package com.hp.service;

public interface IUserService {
	int login(String username,String password);
}
创建Service层实现类UserServiceImpl.java
package com.hp.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hp.dao.IUserDao;
import com.hp.service.IUserService;
/*
 * @Service注解的作用等同于
 * <bean id="IUserService" class="com.hp.service.impl.UserServiceImpl"/>
 */
@Service("IUserService")    //不给值时,默认类名
public class UserServiceImpl implements IUserService {
	
	@Autowired   //不指定值时按名称IUserDao去找
	private IUserDao iUserDao;
	
	public void setiUserDao(IUserDao iUserDao) {
		this.iUserDao = iUserDao;
	}

	@Override
	public int login(String username, String password) {
		// TODO Auto-generated method stub
		return iUserDao.login(username, password);
	}

}
4.创建配置文件ApplicationContext.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:tx="http://www.springframework.org/schema/tx"
       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/tx  http://www.springframework.org/schema/tx/spring-tx.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
		
		<!-- spring注解 -->
		<context:component-scan base-package="com.hp"/>
		<!-- <context:component-scan base-package="com.hp.dao, com.hp.service"/>		 -->
</beans>
5.创建测试类MainTest

package com.hp.test;

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

import com.hp.dao.IUserDao;
import com.hp.dao.impl.UserDaoImpl;
import com.hp.service.IUserService;
import com.hp.service.impl.UserServiceImpl;

public class MainTest {
	
	@Test
	public void login1(){
		String username="admin";
		String password="123";
		ClassPathXmlApplicationContext context=
				new ClassPathXmlApplicationContext("ApplicationContext.xml");
		IUserService iUserService=(IUserService) context.getBean("IUserService");
		int res = iUserService.login(username, password);
		if(res==1){
		System.out.println("登录成功");
		}
		else{
			System.out.println("登录失败");
		}
	}
}

6.项目结构:



三  注解方式的使用

    <!--spring注解-->
    <context:component-scan base-package="包名"/>


常用注解:

@Repository
@Service
@Autowired




@Configuration把一个类作为一个IoC容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean。
@Scope注解 作用域
@Lazy(true) 表示延迟初始化
@Service用于标注业务层组件(注入dao)
@Controller用于标注控制层组件(注入服务)
@Repository用于标注数据访问组件,即DAO组件(实现dao访问)。
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。(把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)
@Scope用于指定scope作用域的(用在类上)
@PostConstruct用于指定初始化方法(用在方法上)
@PreDestory用于指定销毁方法(用在方法上)
@DependsOn:定义Bean初始化及销毁时的顺序
@Primary:自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者,否则将抛出异常
@Autowired 默认按类型装配,如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下:
@Autowired @Qualifier("personDaoBean") 存在多个实例配合使用

@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。
@PostConstruct 初始化注解
@PreDestroy 摧毁注解 默认 单例  启动就加载
@Async异步方法调用


posted @ 2017-10-30 21:33  ·卿欢·  阅读(169)  评论(0编辑  收藏  举报