spring

 

 

 

 

依赖注入------


package com.demo.spring1;


import javax.xml.ws.soap.Addressing;
import java.util.*;

public class student {
   private String name;
   private Address address;
   private String[] books;
   private List<String> hobbys;
   private Map<String,String>  card;
   private Set<String> games;
   private Properties info;

   public String getName() {
       return name;
  }

   public void setName(String name) {
       this.name = name;
  }

   public Address getAddress() {
       return address;
  }

   public void setAddress(Address address) {
       this.address = address;
  }

   public String[] getBooks() {
       return books;
  }

   public void setBooks(String[] books) {
       this.books = books;
  }

   public List<String> getHobbys() {
       return hobbys;
  }

   public void setHobbys(List<String> hobbys) {
       this.hobbys = hobbys;
  }

   public Map<String, String> getCard() {
       return card;
  }

   public void setCard(Map<String, String> card) {
       this.card = card;
  }

   public Set<String> getGames() {
       return games;
  }

   public void setGames(Set<String> games) {
       this.games = games;
  }

   public Properties getInfo() {
       return info;
  }

   public void setInfo(Properties info) {
       this.info = info;
  }

   public String getWife() {
       return wife;
  }

   public void setWife(String wife) {
       this.wife = wife;
  }

   private String wife;

   @Override
   public String toString() {
       return "student{" +
               "name='" + name + '\'' +
               ", address=" + address.toString() +
               ", books=" + Arrays.toString(books) +
               ", hobbys=" + hobbys +
               ", card=" + card +
               ", games=" + games +
               ", info=" + info +
               ", wife='" + wife + '\'' +
               '}';
  }
}
配置xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
   <bean id="Address" class="com.demo.spring1.Address">
       <property name="address" value="西安"></property>
   </bean>
   <bean id="student" class="com.demo.spring1.student">
           <property name="name" value="wanglong"/>
       <property name="address" ref="Address"/>
       <property name="books">
           <array>
               <value>1</value>
               <value>2</value>
               <value>3</value>
           </array>
       </property>
       <property name="hobbys" >
           <list>
               <value>听歌</value>
               <value>敲代码</value>
               <value>看电影</value>
           </list>
       </property>
       <property name="card">
           <map>
               <entry key="身份证" value="222313423"/>
               <entry key="银行卡" value="223353534324"/>
           </map>
       </property>
       <property name="games">
           <set>
               <value>lol</value>
               <value>cf</value>
               <value>coc</value>
           </set>
       </property>
       <property name="wife">
          <null/>
       </property>
       <property name="info">
           <props>
               <prop key="学号">2021</prop>
               <prop key="性别"></prop>
           </props>
       </property>




      </bean>
</beans>

常用依赖

<dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
   <scope>test</scope>
</dependency>


<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>5.1.10.RELEASE</version>
</dependency>
</dependencies>

注释说明

加约束

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd">

</beans>
//开启注解支持
<context:annotation-config/>

@autowired:自动装配通过类型->名字

@resource:自动装配通过名字->类型

@Autowired与@Resource异同:

\1. @Autowired与@Resource都可以用来装配bean。都可以写在字段上,或写在setter方法上。

\2. @Autowired默认按类型装配(属于spring规范),默认情况下必须要求依赖对象必须存在,如果

要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我

们想使用名称装配可以结合@Qualififier注解进行使用

\3. @Resource(属于J2EE复返),默认按照名称进行装配,名称可以通过name属性进行指定。如果

没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在

setter方法上默认取属性名进行装配。 当找不到与名称匹配的bean时才按照类型进行装配。但是

需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。

它们的作用相同都是用注解方式注入对象,但执行顺序不同。@Autowired先byType,@Resource先

byName。

属性注入

<!--指定注解扫描包--> <context:component-scan base-package="com.kuang.pojo"/>

@Component("")可以不用提供set方法,直接在直接名上添加@value("值")

衍生注解

我们这些注解,就是替代了在配置文件当中配置步骤而已!更加的方便快捷!

@Component**三个衍生注解**

为了更好的进行分层,Spring可以使用其它三个注解,功能一样,目前使用哪一个功能都一样。

@Controller:web层

@Service:service层

@Repository:dao层

写上这些注解,就相当于将这个类交给Spring管理装配了!

 

aop注入依赖

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
posted @ 2022-01-05 01:51  爱笙灬  阅读(106)  评论(0)    收藏  举报