引入pom坐标
<dependencies>
<!--spring-mvc-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
</dependencies>
spring对类中的属性的注入
Address类 随便写的一个地址类
@Data@AllArgsConstructor@NoArgsConstructor
public class Address {
public String address;
}
Student类 中定义了很多属性 就是在这些属性中定义参数
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
@Data@AllArgsConstructor@NoArgsConstructor
public class Student {
private Address address;
private String name;
private String[] books;
private String wife;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private Properties info;
}
在spring配置文件中注入
<?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">
<!--java类注入成bean-->
<bean id="address" class="com.cong.add.Address">
<property name="address" value="常山"/>
</bean>
<!-- java类注入成bean -->
<bean id="student" class="com.cong.add.Student">
<!-- 普通值注入-->
<property name="name" value="赵云"></property>
<!-- 引用类型注入-->
<property name="address" ref="address"/>
<!-- 数组-->
<property name="books">
<array>
<value>三国</value>
<value>无双</value>
<value>战将</value>
</array>
</property>
<!-- null注入-->
<property name="wife">
<null></null>
</property>
<!-- list-->
<property name="hobbys">
<list>
<value>抽烟</value>
<value>喝酒</value>
<value>烫头</value>
</list>
</property>
<!-- Map-->
<property name="card">
<map>
<entry key="身份" value="三国第一战将"/>
<entry key="战绩" value="百战百胜"/>
</map>
</property>
<!-- set-->
<property name="games">
<set>
<value>LOL</value>
<value>coc</value>
<value>BOB</value>
</set>
</property>
<!-- Properties -->
<property name="info">
<props>
<prop key="时期">汉末</prop>
</props>
</property>
</bean>
</beans>
测试
import com.cong.add.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
@org.junit.Test
public void name() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContet.xml");
Student student = context.getBean("student", Student.class);
System.out.println("student = " + student);
/*
student = Student(address=Address(address=常山),
name=赵云, books=[三国, 无双, 战将], wife=null, hobbys=[抽烟, 喝酒, 烫头],
*/
}
}
P:命名空间注入 引入p约束xmlns:p="http://www.springframework.org/schema/p"
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.cong.add.User" p:age="18" p:name="周冬雨"></bean>
</beans>
User类
package com.cong.add;
public class User {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
测试
@org.junit.Test
public void user() {
ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml");
User user = context.getBean("user", User.class);
System.out.println("user = " + user);
//user = User{age=18, name='周冬雨'}
}
}
C:命名空间 引入c约束
xmlns:c="http://www.springframework.org/schema/c"
注意 :
C命名空间必须是构造器注入 类必须写构造器
P命名空间是Set方法注入
C:下表示有参构造器的第几个参数
也有名字 可以对应名字赋值
Bean Scope dean的作用域
- prototype 原型 每次都会创建一个对象
- singleton 单例模式
- request
- session
byName类型
注意 :必须是id和自己对象的set方法名一样
<?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="cat" class="com.cong.Cat"></bean>
<bean id="dog" class="com.cong.Dog"></bean>
<!--
autowire="byName" 在本类中寻找和Set方法后面的名字一样的bean 的ids 如果找到了 就会自动装配 找不到就装配不上
-->
<bean id="people" class="com.cong.People" autowire="byName">
</bean>
</beans>
byTybe类型
注意:如果有两个类型一样的bean 就会报错失效
<?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="cat" class="com.cong.Cat"></bean>
<bean id="dog" class="com.cong.Dog"></bean>
<!-- <bean id="dog" class="com.cong.Dog"></bean>-->
<!--
autowire="byName" 在容器中寻找和Set方法后面的名字一样的bean 的ids 如果找到了 就会自动装配 找不到就装配不上
autowire="byType" 在容器中寻找和自己对象属性类型一样的bean 如果找到了 就会自动装配 找不到就装配不上 弊端:如果有两个类型一样的bean就会失效 跑错
-->
<bean id="people" class="com.cong.People" autowire="byType">
</bean>
</beans>
注解自动装配
第一步:引入context约束
第二步:开启自动装配
<!-- 开启注解自动配置-->
<context:annotation-config></context:annotation-config>
<?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:annotation-config>
<bean id="cat" class="com.cong.Cat"></bean>
<bean id="dog" class="com.cong.Dog"></bean>
<bean id="people" class="com.cong.People"></bean>
</beans>
第三步:加注解
@Autowired
public class People {
@Autowired
private Dog dog;
@Autowired
private Cat cat;
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
@Override
public String toString() {
return "People{" +
"dog=" + dog +
", cat=" + cat +
'}';
}
}
spring5的新特性
20.1 Spring5新增一些注解
名称 含义 可标注位置 @Nullable 可以为空 @Target({ElementType.*METHOD*, ElementType.*PARAMETER*, ElementType.*FIELD*} @NonNull 不应为空 @Target({ElementType.*METHOD*, ElementType.*PARAMETER*, ElementType.*FIELD*}) @NonNullFields 在特定包下的字段不应为空 @Target(ElementType.*PACKAGE*) @TypeQualifierDefault(ElementType.*FIELD*) @NonNullApi 参数和方法返回值不应为空 @Target(ElementType.*PACKAGE*) @TypeQualifierDefault({ElementType.*METHOD*, ElementType.*PARAMETER*})
以@Nullable注解为例
@Nullable 注解可以使用在方法上面,属性上面,参数前面,表示方法返回可以为空,属性值可以为空,参数值可以为空。
此注解通常用来消除NullPointerException
Spring5整合Log4j2
导入jar包
<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.11.2</version> <scope>test</scope> </dependency>
编写log4j2的配置文件
<?xml version="1.0" encoding="UTF-8"?> <!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL --> <!--Configuration后面的status用于设置log4j2自身内部的信息输出,可以不设置,当设置成trace时,可以看到log4j2内部各种详细输出--> <configuration status="INFO"> <!--先定义所有的appender--> <appenders> <!--输出日志信息到控制台--> <console name="Console" target="SYSTEM_OUT"> <!--控制日志输出的格式--> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </console> </appenders> <!--然后定义logger,只有定义了logger并引入的appender,appender才会生效--> <!--root:用于指定项目的根日志,如果没有单独指定Logger,则会使用root作为默认的日志输出--> <loggers> <root level="DEBUG"> <appender-ref ref="Console"/> </root> </loggers> </configuration>
Spring5整合Junit5
导入jar包:注意将junit4的相关jar包移除
<!--spring-test--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.1</version> </dependency> <!--junit5--> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.7.2</version> <scope>test</scope> </dependency>
在测试类上添加注解
方式一
@ExtendWith(SpringExtension.class) @ContextConfiguration(locations = "classpath:beans-tx.xml") public class Junit5Test {}
方式二
@SpringJUnitConfig(locations = "classpath:beans-tx.xml") public class Junit5Test {}