Spring入门详细教程(二)

举报
经典鸡翅 发表于 2022/02/17 22:42:20 2022/02/17
【摘要】 前言 本篇紧接着spring入门详细教程(一),建议阅读本篇前,先阅读第一篇。链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/p/10165538.html 一、spring注入方式 1、set方法注入 <bean name="us...

前言

本篇紧接着spring入门详细教程(一),建议阅读本篇前,先阅读第一篇。链接如下:

Spring入门详细教程(一) https://www.cnblogs.com/jichi/p/10165538.html

一、spring注入方式

1、set方法注入


    
  1. <bean name="user" class="com.jichi.entity.User" >
  2.   <property name="name" value="小明"></property>
  3.   <property name="age" value="18"></property>
  4. </bean>

2、构造方法注入


    
  1. <bean name="user" class="com.jichi.entity.User" >
  2. <constructor-arg name="name" value="小红" ></constructor-arg>
  3.   <constructor-arg name="age" value="50"></constructor-arg>
  4. </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表达式注入


    
  1. <bean name="user" class="com.jichi.entity.User">
  2. <property name="name" value="小红"></property>
  3. <property name="age" value="18"></property>
  4. </bean>
  5. <bean name="user1" class="com.jichi.entity.User">
  6. <property name="name" value="#{user.name}"></property>
  7. <property name="age" value="#{user.age}"></property>
  8. </bean>

二、spring复杂类型注入


    
  1. public class Collection {
  2. public String[] arr;
  3. public List<String> list;
  4. public Map<String,Object> map;
  5. public Properties props;
  6. public String[] getArr() {
  7. return arr;
  8. }
  9. public void setArr(String[] arr) {
  10. this.arr = arr;
  11. }
  12. public List<String> getList() {
  13. return list;
  14. }
  15. public void setList(List<String> list) {
  16. this.list = list;
  17. }
  18. public Map<String, Object> getMap() {
  19. return map;
  20. }
  21. public void setMap(Map<String, Object> map) {
  22. this.map = map;
  23. }
  24. public Properties getProps() {
  25. return props;
  26. }
  27. public void setProps(Properties props) {
  28. this.props = props;
  29. }
  30. @Override
  31. public String toString() {
  32. return "Collection [arr=" + Arrays.toString(arr) + ", list=" + list + ", map=" + map + ", props=" + props + "]";
  33. }
  34. }

1、数组类型注入


    
  1. <bean name="collect" class="com.jichi.entity.Collection">
  2. <property name="arr">
  3. <array>
  4. <value>xiaohei</value>
  5. <value>xiaobai</value>
  6. </array>
  7. </property>
  8. </bean>

2、list类型注入


    
  1. <bean name="collect" class="com.jichi.entity.Collection">
  2. <property name="list">
  3. <list>
  4. <value>xiaohei</value>
  5. <value>xiaobai</value>
  6. </list>
  7. </property>
  8. </bean>

3、map类型注入


    
  1. <bean name="collect" class="com.jichi.entity.Collection">
  2. <property name="map">
  3. <map>
  4. <entry key="name" value="xiaohei"></entry>
  5. <entry key="age" value="18"></entry>
  6. </map>
  7. </property>
  8. </bean>

4、properties类型注入


    
  1. <bean name="collect" class="com.jichi.entity.Collection">
  2. <property name="props">
  3. <props>
  4. <prop key="name">xiaohei</prop>
  5. <prop key="age">18</prop>
  6. </props>
  7. </property>
  8. </bean>

三、配置spring随web项目启动初始化

在web.xml中配置。


    
  1. <listener>
  2. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  3. </listener>

    
  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>classpath:applicationContext.xml</param-value>
  4. </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、在类上添加注解


    
  1. @Component
  2. public class User {
  3. }

六、spring常用注解

1、@Componet,@Controller,@Service,@Repository四个组件注解,作用在类上。四个注解并无区别,只是为了方便区分。

2、@Scope注解,作用在类上。


    
  1. @Scope(scopeName="singleton") //单例模式
  2. public class User {
  3. }

    
  1. @Scope(scopeName="prototype") //多例模式
  2. public class User {
  3. }

3、@Value用于注入普通类型值

第一种方式:作用在属性上,通过反射的filed值,破坏了对象的封装性。


    
  1. @Value("xiaohei")
  2. private String name;

第二种方式:通过set方法赋值,不破坏对象的封装性。


    
  1. @Value("xiaobai")
  2. public void setName(String name) {
  3. this.name = name;
  4. }

4、@Autowired,@Resource,@Qualifier注解 

引用类型的装配方式,详细区别请看之前的博客。


    
  1. @Autowired
  2. private Car car;

    
  1. @Resource
  2. private Car car;

5、@PostConstruct与@PreDestroy


    
  1. @PostConstruct //创建对象前调用
  2. public void init(){
  3. System.out.println("初始");
  4. }
  5. @PreDestroy   //对象销毁前调用
  6. public void destory(){
  7. System.out.println("销毁");
  8. }

七、spring与junit整合测试

1、导入spring基础包,与aop包和test包,可从lib中找到。

2、在测试类上添加注解


    
  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration("classpath:applicationContext.xml")
  3. public class TestJunit {
  4. @Resource
  5. private User user;
  6. @Test
  7. public void test1(){
  8. System.out.println(user);
  9. }
  10. }

文章来源: blog.csdn.net,作者:经典鸡翅,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/hanqing456/article/details/111879487

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。