spring5.0学习笔记5

1.springIOC常用的注解
2.案例使用xml方式和注解方式实现表单的CRUD操作
3.改造基于注解的IOC案例,使用注解的方式实现
4.spring和Juit的整合

/**
 * 用于创建对象的注解
 */
package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

public class Test{
    public static void main(String[] args){
    	//1.获取核心容器对象
    	ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
    	//2.根据id获取bean对象
    	IAccountService as = (IAccountService)ac.getBean("accountService");
    	System.out.println(as);
    	IAccountDao adao = (IAccountDao)ac.getBean("accountDao");
    	System.out.println(adao);
    	as.saveAccount();
    }
}

interface IAccountService{
	/**
	 * 模拟保存账户
	 */
	public void saveAccount();
}

/**
 * 业务层实现类
 * 曾经的配置<bean id="accountService" class="com.test.AccountServiceImp"
 * 		scope="" init-method=" " destroy-method="">
 * 		<property name="" value="" ref=""></property>
 * </bean>
 *
 * 用于创建对象的注解
 * 用于注入数据的注解
 * 用于改变作用范围的注解
 * 和生命周期相关的
 * 
 *  @Component
 *  	作用:用于把当前类对象存入spring容器
 *  	属性:
 *  		value:用于指定bean的id,默认值时当前类名首字母改小写
 *  @Controller  表现层
 *  @Service     业务层
 *  @Repository  持久层
 */
@Component(value="accountService")
class AccountServiceImp implements IAccountService{
	public AccountServiceImp(){
		System.out.println("对象创建了");
	}
	public void saveAccount(){
		IAccountDao adao = new accountDaoImp();
		adao.saveAccount();
	 }
}

interface IAccountDao{
	public void saveAccount();
}

@Repository("accountDao")
class accountDaoImp implements IAccountDao{
	public void saveAccount(){
		System.out.println("save a account");
	}
}
2020-3-25 11:26:14 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d]; startup date [Wed Mar 25 11:26:14 CST 2020]; root of context hierarchy 2020-3-25 11:26:14 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [bean.xml] 2020-3-25 11:26:14 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d]: org.springframework.beans.factory.support.DefaultListableBeanFactory@453807 2020-3-25 11:26:14 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@453807: defining beans [accountDao,accountService,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor]; root of factory hierarchy

对象创建了
com.test.AccountServiceImp@e06940
com.test.accountDaoImp@11e0c13
save a account

/**
 * 用于注入数据的注解
 */
package com.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

public class Test{
    public static void main(String[] args){
    	//1.获取核心容器对象
    	ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
    	//2.根据id获取bean对象
    	IAccountService as = (IAccountService)ac.getBean("accountService");
    	System.out.println(as);
    	IAccountDao adao = (IAccountDao)ac.getBean("accountDao");
    	System.out.println(adao);
    	as.saveAccount();
    }
}

interface IAccountService{
	/**
	 * 模拟保存账户
	 */
	public void saveAccount();
}

/**
 * 业务层实现类
 * 曾经的配置<bean id="accountService" class="com.test.AccountServiceImp"
 * 		scope="" init-method=" " destroy-method="">
 * 		<property name="" value="" ref=""></property>
 * </bean>
 *
 * 用于创建对象的注解
 * 用于注入数据的注解
 * 		Autowired
 * 			作用:自动按照类型注入,set方法就不是必要的
 * 		Qualifyer
 * 			在按照类型注入的基础上,按照名称注入
 * 		Resource 
 * 			按照名称注入
 * 
 * 用于注入基本数据类型的注解
 * 		Value,可以使用spring中的SpEL ${}
 * 用于改变作用范围的注解
 * 		Scope,用于指定bean的作用范围
 * 和生命周期相关的
 * 		Predestory:用于指定销毁方法
 * 		PostConstructor 用于指定初始化方法
 */
@Component(value="accountService")
class AccountServiceImp implements IAccountService{
//	@Autowired
//	@Qualifier("accountDao")
	//该spring版本没有没有@Resource注解
	private IAccountDao accountDao; //accountDao和容器寸的key相同
	public AccountServiceImp(){
		System.out.println("对象创建了");
	}
	public void saveAccount(){
		accountDao.saveAccount();
	 }
}

interface IAccountDao{
	public void saveAccount();
}

@Repository("accountDao")
class accountDaoImp implements IAccountDao{
	public void saveAccount(){
		System.out.println("save a account");
	}
}

2020-3-25 11:31:22 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d]; startup date [Wed Mar 25 11:31:22 CST 2020]; root of context hierarchy 2020-3-25 11:31:23 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [bean.xml] 2020-3-25 11:31:23 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d]: org.springframework.beans.factory.support.DefaultListableBeanFactory@618d26 2020-3-25 11:31:23 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@618d26: defining beans [accountDao,accountService,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor]; root of factory hierarchy 对象创建了 com.test.AccountServiceImp@1a68ef9 com.test.accountDaoImp@1f48262 save a account
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值