Spring入门(IOC)

1.建立Maven并导入依赖

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.9</version>
        </dependency>

2.编写实体类(Set方法)
如:

public class Test {
    
    
    private String Name;

    public String getName() {
    
    
        return Name;
    }

    public void setName(String name) {
    
    
        Name = name;
    }
      public String toString() {
    
    
        return "Test{" +
                "Name='" + Name + '\'' +
                '}';
    }
}

3.编写Beans.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="自命名" class="实体类的路径">
       <property name="对应实体类的属性" value="属性值"/>
   </bean>
   <!--例-->
    <bean id="hello" class="com.Sky.Entity.Test">
       <property name="name" value="SSSSSSSSSSSSS"/>
   </bean>
</beans>


4.编写测试函数
通过ApplicationContext context = new ClassPathXmlApplicationContext("配置文件路径");来创建对象,并调用
如:

public class Test01 {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        System.out.println(context.getBean("hello"));
    }
}

5.IOC创建对象

<!--无参构造创建对象-->
 <bean id="hello" class="com.Sky.Entity.Test">
       <property name="name" value="SSSSSSSSSSSSS"/>
   </bean>
  <!--有参构造创建对象-->
  <!--1.下标复制-->
   <bean id="hello" class="com.Sky.Entity.Test">
       <constructor-arg index="0" value="SSSSSS"/>
   </bean>
   <!--2.通过类型创建-->
     <bean id="hello" class="com.Sky.Entity.Test">
       <constructor-arg type="java.lang.String" value="SSSSSSS"/>
   </bean>
   <!--3.直接通过参数名创建-->
    <bean id="hello" class="com.Sky.Entity.Test">
        <constructor-arg name="name" value="SSSSS"/>
   </bean>
   <!--在配置文件加载时,容器中管理的对象已经初始化完毕>

6.依赖注入

<!--常用类型的注入-->
 <bean id="自定义唯一标识" class="实体类位置">
 				<!--普通注入:基本数据类型-->
            <property name="属性" value=""/>
            <!--Bean注入:引用类-->
            <property name="属性" ref="另一个类的属性"/>
            <!--数组类型-->
            <property name="属性">
                <array>
                    <value></value>
                    <value></value>
                </array>
            </property>
             <!--list类型-->
            <property name="属性">
                <list>
                    <value></value>
                    <value></value>
                </list>
            </property>
             <!--map类型-->
            <property name="属性">
                <map>
                   <entry key="" value=""/>
                    <entry key="" value=""/>
                </map>
            </property>
            <property name="属性">
                <set>
                    <value></value>
                    <value></value>
                </set>
            </property>
            <property name="属性">
                <props>
                    <prop key=""></prop>
                </props>
            </property>
        </bean>

例:

        <bean id="hello" class="com.Sky.Entity.Test">
            <property name="name" value="sss"/>
            <property name="address" ref="asd"/>
            <property name="books">
                <array>
                    <value>书名1</value>
                    <value>书名2</value>
                </array>
            </property>
            <property name="hobby">
                <list>
                    <value>敲代码</value>
                    <value>听歌</value>
                </list>
            </property>
            <property name="card">
                <map>
                   <entry key="1" value="1312"/>
                    <entry key="2" value="1234"/>
                </map>
            </property>
            <property name="games">
                <set>
                    <value>lol</value>
                    <value>timi</value>
                </set>
            </property>
            <property name="info">
                <props>
                    <prop key="driver">comcccc</prop>
                </props>
            </property>
        </bean>

7.Bean的自动装配

                          <!--Bean的注入:引用类-->
<!--普通方法-->
<bean class="类1" id="id1"/>
    <bean class="类2" id="id2"/>
    <bean class="类3" id="id3">
        <property name="对应类1的对象" ref="id1"/>
        <property name="对应类2的对象" ref="id2"/>
    </bean>
 <!--例-->
 <bean class="com.Sky.Entity.Cat" id="Cat"/>
    <bean class="com.Sky.Entity.Dog" id="dog"/>
    <bean class="com.Sky.Entity.People" id="people" autowire="byName">
        <property name="cat" ref="Cat"/>
        <property name="dog" ref="dog"/>
    </bean>
 <根据名字自动装配:autowire="byName">
 <!--例-->
 <bean class="com.Sky.Entity.Cat" id="cat"/>
    <bean class="com.Sky.Entity.Dog" id="dog"/>
    <bean class="com.Sky.Entity.People" id="people" autowire="byName">
    <!可省略property,根据名字自动装配,他会根据你想要注入的类中的Set方法名里出去Set的部分,和id 进行匹配,如果有相同的则自动装配>
    </bean>
   <根据类型进行自动装配:autowire="byType">
   <>
     <bean class="com.Sky.Entity.Cat" />
    <bean class="com.Sky.Entity.Dog"/>
    <bean class="com.Sky.Entity.People" id="people" autowire="byType">
    <!通过class指向的类型进行装配,用不到的id可以省略>
    </bean>

8.注解的使用

<1.更换或修改配置文件如下>
<?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">
   <context:annotation-config/>
       <!--扫描指定的包,其包下面的注解就会生效-->
  <context:component-scan base-package="包名/位置"/>
 

</beans>
<2.实现自动装配:@Autowired的简单使用>
   <bean class="类1" id="id1"/>
    <bean class="类2" id="id2"/>
    <bean class="类3" id="id3"/>
    若使bean1与bean2注入到bean3中则在bean3的类中添加注解
    <例如:使用@Autowired-->
    <bean id="cat" class="com.Sky.Entity.Cat"/>
    <bean id="dog" class="com.Sky.Entity.Dog"/>
    <bean id="people" class="com.Sky.Entity.People"/>
 public class People {
    @Autowired
    private Dog dog;
    @Autowired
    private Cat cat;
    private String people;
    }
<需要保证@Autowired注解的类型在bean中出现,其会根据类型进行匹配,若出现多个相同的类型,则根据名字进行匹配>
<3.component组件>
<在对应的类上加上@component可以代替bean,如:>
@Component
public class People {
 @Value("名字")
private String name;
}
等价于
 <bean id="people" class="com.Sky.Entity.People">
        <property name="name" value="名字"/>
    </bean>
<其中,类名的小写就是id, value里面的值, 等价于 property 中的值>
<注:在Web开发中,根据MVC三层架构分层,一般Component有不同的别名
   1.Dao层 : @Repository
   2.Service层: @Service
   3.Controller层 : @Controller
   虽然名字不同,但功能相同>

Spring中文帮助文档链接:https://www.docs4dev.com/docs/zh/spring-framework/5.1.3.RELEASE/reference/core.html#beans

猜你喜欢

转载自blog.csdn.net/Badman0726/article/details/119998022