前言
本篇紧接着spring入门详细教程(一),建议阅读本篇前,先阅读第一篇。链接如下:
Spring入门详细教程(一) https://www.cnblogs.com/jichi/p/10165538.html
一、spring注入方式
1、set方法注入
- <bean name="user" class="com.jichi.entity.User" >
- <property name="name" value="小明"></property>
- <property name="age" value="18"></property>
- </bean>
2、构造方法注入
- <bean name="user" class="com.jichi.entity.User" >
- <constructor-arg name="name" value="小红" ></constructor-arg>
- <constructor-arg name="age" value="50"></constructor-arg>
- </bean>
3、p名称空间注入
- xmlns:p="http://www.springframework.org/schema/p"
- <bean name="user" class="com.jichi.entity.User" p:name="小白" p:age="10"></bean>
4、spel表达式注入
- <bean name="user" class="com.jichi.entity.User">
- <property name="name" value="小红"></property>
- <property name="age" value="18"></property>
- </bean>
- <bean name="user1" class="com.jichi.entity.User">
- <property name="name" value="#{user.name}"></property>
- <property name="age" value="#{user.age}"></property>
- </bean>
二、spring复杂类型注入
- public class Collection {
- public String[] arr;
- public List<String> list;
- public Map<String,Object> map;
- Properties props;
- String[] getArr() {
- return arr;
- }
- void setArr(String[] arr) {
- this.arr = getList() {
- list;
- }
- void setList(List<String> list) {
- this.list = getMap() {
- map;
- }
- void setMap(Map<String,1)"> map) {
- this.map = Properties getProps() {
- props;
- }
- setProps(Properties props) {
- this.props = props;
- }
- @Override
- String toString() {
- return "Collection [arr=" + Arrays.toString(arr) + ",list=" + list + ",map=" + map + ",props=" + props + "]";
- }
- }
1、数组类型注入
- <bean name="collect" class="com.jichi.entity.Collection">
- <property name="arr">
- <array>
- <value>xiaohei</value>
- <value>xiaobai</value>
- </array>
- </property>
- </bean>
2、list类型注入
- <bean name="collect" class="com.jichi.entity.Collection">
- <property name="list">
- <list>
- <value>xiaohei</value>
- <value>xiaobai</value>
- </list>
- </property>
- </bean>
3、map类型注入
- <bean name="collect" class="com.jichi.entity.Collection">
- <property name="map">
- <map>
- <entry key="name" value="xiaohei"></entry>
- <entry key="age" value="18"></entry>
- </map>
- </property>
- </bean>
4、properties类型注入
- <bean name="collect" class="com.jichi.entity.Collection">
- <property name="props">
- <props>
- <prop key="name">xiaohei</prop>
- <prop key="age">18</prop>
- </props>
- </property>
- </bean>
三、配置spring随web项目启动初始化
在web.xml中配置。
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- </context-param>
四、spring的分配置文件
方式一:
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext1.xml","applicationContext2.xml")
方式二:
- <import resource="applicationContext.xml"></import>
五、spring注解配置
1、开启注解扫描
- <context:component-scan base-package="com.jichi.entity"></context:component-scan>
扫描com.jichi.entity下的所有类中的注解。
2、在类上添加注解
- @Component
- User {
- }
六、spring常用注解
1、@Componet,@Controller,@Service,@Repository四个组件注解,作用在类上。四个注解并无区别,只是为了方便区分。
2、@Scope注解,作用在类上。
- @Scope(scopeName="singleton") //单例模式
- User {
- }
- @Scope(scopeName="prototype") //多例模式
- User {
- }
3、@Value用于注入普通类型值
第一种方式:作用在属性上,通过反射的filed值,破坏了对象的封装性。
- @Value("xiaohei")
- private String name;
第二种方式:通过set方法赋值,不破坏对象的封装性。
- @Value("xiaobai")
- setName(String name) {
- this.name = name;
- }
4、@Autowired,@Resource,@Qualifier注解
引用类型的装配方式,详细区别请看之前的博客。
- @Autowired
- private Car car;
- @Resource
- private Car car;
5、@PostConstruct与@PreDestroy
七、spring与junit整合测试
1、导入spring基础包,与aop包和test包,可从lib中找到。
2、在测试类上添加注解
- @RunWith(SpringJUnit4ClassRunner.)
- @ContextConfiguration("classpath:applicationContext.xml" TestJunit {
- @Resource
- private User user;
- @Test
- test1(){
- System.out.println(user);
- }
- }