spring笔记1

1.pom.xml
(1).jar
(2).project object model
(3).spring-framework-3.2.x


2.applicationContext.xml
(1).WEB-INF/
(2).bean
(3).<bean id = "userDao" class="xxx.xxx.xxx.dao.impl.UserDaoImpl" />
(4).<bean id = "userService" class="xxx.xxx.xxx.dao.impl.UserServiceImpl" scope="xx" />
<property name="userDao" ref="userDao">//name=setXxx() ref=class.id
    </bean>
(5).ClassPathXmlApplicationContext:src/applicationContext.xml
    FileSystemXmlApplicationContext:WEB-INF/applicationContext.xml
(6).scope=singletom(default) create when factory build
    scope=prototype          create when invoke method


Get Bean
->1.xml
<bean id="emp" class="xxx.xx.domain.Emp">
<constructor-arg index="0" value="1"></..>

<constructor-arg name="id" value="1"></..>
<constructor-arg name="name" value="mike"></..>

<constructor-arg type="java.lang.Integer" value="1"></..>
<constructor-arg type="java.lang.String" value="mike"></..>
</bean>


->2.setter(ok)
<bean id = "userService" class="xxx.xxx.xxx.dao.impl.UserServiceImpl" scope="xx" />
<bean id = "userService" class="xxx.xxx.xxx.dao.impl.UserServiceImpl" init-method="init" destory-method="destory" />
<property name="userDao" ref="userDao">//name=setXxx() ref=class.id
</bean>


->3.static factory
<bean id="empBean" class="xx.xx.EmpBeanFactory" factory-method="getEmp" ></bean>
public class EmpBeanFactory{
public static Emp getEmp(){
return new Emp(1,"mike");
}
}


->4.instance factory(ok)
<bean id="empBean" class="xx.xx.EmpBeanFactory"></bean>
public class EmpBeanFactory implements FactoryBean<Emp>{
public Emo getObject() throws Exception{
return new Emp(1,"alan");
}
}
// Spring容器发现配置Bean class是FactoryBean 自动调用getObject方法返回 !




3.web.xml
(1).ContextLoaderListener/ContextLoaderServlet -->define loadLocation eg: spring.web
listener cannot use in servlet 2.2
(2).context-param --configlocation:diapatcher-servlet.xml
(3).servlet
(4).servlet-mapping


4.diapatcher-servlet.xml
--springmvc
(1).scan package
(2).bean:mvc.annotation.defaultAnnotationHandlerMapping
(3).bean:mvc.annotation.AnnotationMethodHandlerAdapter
(4).bean:viewResolver


5.
org.apache.commons.logging-1.1.1.jar
org.apache.log4j-1.2.15.jar
spring - core *4


6.
src/log4j.properties
applicationContext.xml


7.spEL (spring expression language)
#{}  id,name,method
<bean id="emp" class="xxx.xx.domain.Emp" p:id="1010" p:name="#{book.name}"  >




//=================================================
注解方式装配
bean实例注册,由注解完成。@component


由于Bean在不同的包下面所以要进行扫描
1.开启注解,注解才能生效
<context>名称空间
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd


2.告诉spring扫描那些包
<context:component-scan base-package="xx.xx.xx.dao,xx.xx.xx.service"><context:component-scan>




3.@component
=@controller
=@service
=@repository


4.注入
(1).简单类型@Value("mike")
(2).复杂对象@Value("#{userDao}") -- spEL表达式
@Autowired -- 类型
@Qualifier("userDao") -- 名称
@Resource(name = "userDao") -- 名称
@Inject -- 类型
@Inject @Named("userDao") -- 名称


5.初始化,销毁
@PostConstruct = xml中的 init-method
@PreDestory    = xml中的 destory-method
在方法上加上


6.作用域scope
@Scope(value="prototype") -- 默认singleton




//-------------------mix时代--------------------
bean定义:xml
bean关系:注解


1.激活
<context:annotation-config />

<context:componet-scan> 包括 <context:annotation-config> 功能
(使@repository @postconstruct @predestory @autowired生效)


2.bean定义:xml
<bean id = "userService" class="xxx.xxx.xxx.dao.impl.UserServiceImpl"/>


3.bean关系:注解
@Autowired
private UserDao userDao;




//-----------------------测试------------------------
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class aatest{
@Autowired
private xxService xxservice;

@Test
public void demo(){
....
}
}


1.spring-test.jar/junit
2.new ClassPathXmlApplicationContext 
3.测试不需要<context:annotation-config>可以直接!Autowired



















































整理自尚硅谷视频教程springboot高级篇,并增加部分springboot2.x的内容 一、Spring Boot与缓存 一、JSR107 Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry 和 Expiry。 • CachingProvider定义了创建、配置、获取、管理和控制多个CacheManager。一个应用可 以在运行 期访问多个CachingProvider。 • CacheManager定义了创建、配置、获取、管理和控制多个唯一命名 的Cache,这些Cache 存在于CacheManager的上下文中。一个CacheManager仅被一个 CachingProvider所拥有。 • Cache是一个类似Map的数据结构并临时存储以Key为索引的值。一个 Cache仅被一个 CacheManager所拥有。 • Entry是一个存储在Cache中的key-value对。 • Expiry 每一 个存储在Cache中的条目有一个定义的有效期。一旦超过这个时间,条目为过期 的状态。一旦过期,条 目将不可访问、更新和删除。缓存有效期可以通过ExpiryPolicy设置。 二、Spring缓存抽象 Spring从3.1开始定义了org.springframework.cache.Cache 和 org.springframework.cache.CacheManager接口来统一不同的缓存技术; 并支持使用JCache(JSR- 107)注解简化我们开发; • Cache接口为缓存的组件规范定义,包含缓存的各种操作集合; • Cache接 口下Spring提供了各种xxxCache的实现;如RedisCache,EhCacheCache , ConcurrentMapCache 等; • 每次调用需要缓存功能的方法时,Spring会检查检查指定参数的指定的目标方法是否 已经被调用 过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法 并缓存结果后返回给用户。下 次调用直接从缓存中获取。 • 使用Spring缓存抽象时我们需要关注以下两点; 1、确定方法需要被缓存 以及他们的缓存策略 2、从缓存中读取之前缓存存储的数据 Cache 缓存接口,定义缓存操作。实现有:RedisCache、EhCacheCache、 ConcurrentMapCache等 CacheManager 缓存管理器,管理各种缓存(Cache)组件 @Cacheable 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存 @CacheEvict 清空缓存 @CachePut 保证方法被调用,又希望结果被缓存。 @EnableCaching 开启基于注解的缓存 keyGenerator 缓存数据时key生成策略 serialize 缓存数据时value序列化策略 @CacheConfig 抽取缓存的公共配置 三、几个重要概念&缓存注解 1、常用注解 2、常用参数 名字 位置 描述 示例 methodName root object 当前被调用的方法名 #root.methodName method root object 当前被调用的方法 #root.method.name target root object 当前被调用的目标对象 #root.target targetClass root object 当前被调用的目标对象类 #root.targetClass args root object 当前被调用的方法的参数列表 #root.args[0] 3、常用参数SPEL说明 名字 位置 描述 示例 caches root object 当前方法调用使用的缓存列表(如 @Cacheable(value= {"cache1","cache2"}) ), 则有两 个cache #root.caches[0].name argument name evaluation context 方法参数的名字. 可以直接 #参数 名 ,也可以使用 #p0或#a0 的形 式,0代表参数的索引; #iban 、 #a0 、 #p0 result evaluation context 方法执行后的返回值(仅当方法执 行之后的判断有效,如‘unless’ , ’cache put’的表达式 ’cache evict’的表达式 beforeInvocation=false ) #result 四、代码中使用缓存 1、搭建基本环境 1、导入数据库文件 创建出department和employee表 2、创建javaBean封装数据 3、整合MyBatis操作数据库 1.配置数据源信息 2.使用注解版的MyBatis1)、@MapperScan指定需要扫描的mapper接口所在的包
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值