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
(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