Spring学习笔记1

#Spring笔记
###常用依赖
直接可以用这些来装甲maven项目,导入spring框架

 <dependencies>
        <dependency>
            <!--https://mvnrepository.com/artifact/org.springframework/spring-webmvc-->
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>




####Autowired自动装配—byName,byType


 <bean id="people" class="com.kuang.pojo.People" autowire="byType">
<!--autowire byName:id不能变化,要唯一。这个bean要和set方法的值一致,比如我们这里是dog传递,只能写dog byType:每一个bean唯一才能注入,id可以随意变化。不用写id也能跑,它是根据类型来运行,class属性要唯一 autowire="byType" -->

####ref引用装配

 <bean id="cat" class="com.kuang.pojo.Cat"></bean>
 <bean id="dog" class="com.kuang.pojo.Dog"></bean>
<bean id="people" class="com.kuang.pojo.People" >
       
        <property name="name" value="我是主人"></property>
       
       ref引用,注入上面的dog和cat
        <property name="cat" ref="cat"></property>
        <property name="dog" ref="dog"></property>
        
    </bean>

##注解说明
#####@Autowired:自动装配通过类型和名字,
如果Autowired不能唯一自动装配上属性,
则需要通过@Qualifier(value="****")

#####@Resourse:自动装名字和类型,java原生的注解,
java原生的自动装配注解,先通过id注入,再通过class注入,先检查id,在检查class,通过byType

#####@Nullable:字段标记这个注解,说明这个字段可以为null。

#####@Component:属性的注入
这个组件,放在类上,说明这个类被Spring管理了,代替了bean的注入
注解注入的适合用于简单的数据注入,过于复杂还是用DI注入,

######代码案例

//实体类
package com.kuang.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

//通过注解扫描指定包。等价于<bean id="user" class="com.kuang.pojo.User"></bean>
//Component是组件
@Component
public class User {
   
    //方法一
    @Value("叫爸爸") //等价于 <property name="user" value="baba"></property>
    public String name;

        //方法二,使用set方法注入,Value注解赋值
    @Value("叫爷爷")
    public void setName(String name) {
   
        this.name = name;
    }
}


//测试类

import com.kuang.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest6 {
   
    public static void main(String[] args) {
   
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContent.xml");
        User user = context.getBean("user", User.class);//这里地方装配是扫描之后,注解装配,bean没有id,默认是类的小写
        System.out.println(user.name);
    }
}


配置文件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" 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 https://www.springframework.org/schema/context/spring-context.xsd ">
 <!--扫描指定包下的注解,这个包下的注解就会生效-->
 <context:component-scan base-package="com.kuang.pojo"></context:component-scan>

 <context:annotation-config></context:annotation-config>
 <!-- <bean id="user" class="com.kuang.pojo.User"> <property name="user" value="baba"></property> </bean> -->
</beans>

####衍生的注解
@Component有几个衍生注解。我们在web开发中,会按照mvc三层架构分层。
dao层,@Repository
service层,@Service
controller层,@Controller
用这个三个注解装配时,要把扫描包换成这三个类的目录
<context:component-scan base-package=“com.kuang”></context:component-scan>

#####注解的作用域
@Scope(“singleton”)//标注注解的作用域

###注意事项
在引用注解自动装配要导入content库。

<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 https://www.springframework.org/schema/context/spring-context.xsd ">
    <!--配置注解的支持,context约束,必须要配置 使用注解注入之后,要在属性上添加@autowired。此时可以不用get,set方法 -->
    <context:annotation-config/>

###注入的方式
1.spring-01-ioc1项目

//方法一,(不推荐)通过组合的方式,把userDao获取都UserService
    //private UserDao userDao=new UserDaoOracleImpl();

    //方法二。利用set进行动态实现值的注入
    //这里我们不必在改变接口的实现类,可以直接进行修改对象
 private  UserDao userDao;
    public void setUserDao(UserDao userDao){
   
        this.userDao=userDao;
    }

###bean标签使用
bean就是对象
类型 变量名=new 类型();
Hello hello=new Hello();

id=变量名
class=new 的对象
property相当于对象中的属性设置的一个值。

IOC创建对象的全部方式
id:bean的唯一标识符,也就是相当于我们学的对象名,
class:bean对象所对应的全限定名,包名+类型

####bean赋值,通过有参,无参

<bean id="user" class="com.kuang.pojo.User">
        <!--方法一,通过无参构造来获取 <property name="name" value="李梅影"></property> -->


        <!--方法二,通过有参构造。下标方式来赋值 <constructor-arg index="0" value="陈余龙"></constructor-arg> -->

        <!--方式二。通过有参构造,type类型来赋值,只能type是java.lang.String。当参数类型是两个String就无法传递,不建议使用 <constructor-arg type="java.lang.String" value="爸爸"></constructor-arg> -->

        <!--方式三,直接通过参数名来设置-->
        <constructor-arg name="name" value="xiao li"></constructor-arg>

    </bean>

    <!--在配置文件加载过程中,容器中的管理对象就已经初始化了 通过name也可以取别名(可以取多个),比如user2 分隔符号也是都可以,比如逗号,分号,空格 -->
    <bean id="userT" class="com.kuang.pojo.UserT" name="user2,u2">
        <constructor-arg name="name" value="UserT"></constructor-arg>
    </bean>

    <!--别名,在getBean试,可以直接获取别名来代替user-->
    <alias name="user" alias="DABIAOGE"></alias>

####IOC容器的数据注入方式

private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String, String> card;
    private Set<String> games;
    private String wife;
    private Properties info;//配置文件,以键值对的形式出现,key不能重复
 <bean id="address" class="com.kuang.pojo.Address">
    </bean>

    <bean id="student" class="com.kuang.pojo.Student">
        <!--第一种,DI依赖注入,普通值注入,value-->
        <property name="name" value="chen yu long"></property>

        <!--第二种,Bean注入,ref-->
        <property name="address" ref="address"></property>

        <!-- 数组注入array -->
        <property name="books">
            <array>
                <value>红露面</value>
                <value>西游记</value>
                <value>三国演义</value>
                <value>三国演义新版</value>
            </array>
        </property>

        <!--list注入-->
        <property name="hobbys">
            <list>
                <value>读书</value>
                <value>翘代码</value>
            </list>
        </property>

        <!--map里面是键值对,所以是entry实体-->
        <property name="card">
            <map>
             <entry key="身份证" value="11111"></entry>
                <entry key="银行卡" value="22222"></entry>
            </map>
        </property>

        <!--set注入-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>CF</value>
            </set>
        </property>

                <!--null空-->
        <property name="wife">
            <null></null>
        </property>

        <!--properties是配置类型,value写在外。也是成一个类似的键值对,key唯一-->
        <property name="info">
            <props>
                <prop key="学号">2020</prop>
                <prop key="性别"></prop>
                <prop key="driver">class</prop>
            </props>
        </property>

    </bean>

####c标签和p标签注入
引用文件

 xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
<!--通过p命名注入,可以直接注入属性,property-->
    <bean id="user" class="com.kuang.pojo.User" p:age="18" p:email="123" p:name="李刚">

    </bean>
    <!--通过c命名注入,通过构造器注入,必须要有有参构造和无参构造-->
    <bean id="user2" class="com.kuang.pojo.User" c:name="222" c:email="222" c:age="22" scope="singleton">
    <!--单例模式 singleton,spring默认的单例
    原型模式:prototype


####junit包测试用

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

   //利用junit包进行
    @Test
    public void  test2(){
   
        ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml");
        User user = context.getBean("user2", User.class);//利用反射指定其对象,不用在进行强转
        System.out.println(user.toString());
    }

##小结
xml和注解:
xml是万能的。适用于各种场合,维护简单方便
注解,不是自己的类不能使用,维护要每个类都改,很麻烦

最佳选择效果:
1、xml用来管理bean;

<bean id="address" class="com.kuang.pojo.Address"></bean>

<bean id="student" class="com.kuang.pojo.Student"></bean>


2、注解只负责数据的注入,@Value(""")

@Component
@Scope("singleton")//标注注解的作用域
public class User {
   
    //方法一
    @Value("叫爸爸") //等价于 <property name="user" value="baba"></property>
    public String name;

        //方法二,使用set方法注入,Value注解赋值
    @Value("叫爷爷")
    public void setName(String name) {
   
        this.name = name;
    }
}

我们在使用过程中,要必须开启注解的支持

 <!--扫描指定包下的注解,这个包下的注解就会生效-->
    <context:component-scan base-package="com.kuang"></context:component-scan>

    <context:annotation-config></context:annotation-config>
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务