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



















































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值