注解:给程序看的代码,程序看到后会执行不同的操作
在使用Spring注解开发之前,你需要理解Spring IoC并且会简单使用它:Spring ioC的简单理解与使用_weixin_45506581的博客-CSDN博客
前提条件:一个 web-maven项目
需要加入的依赖 pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Spring_Test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>Spring_Test Maven Webapp</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.14</version>
</dependency>
</dependencies>
<build>
<finalName>Spring_Test</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
注解分类
创建对象注解:@Component(通用注解),@Controller(用于视图层),@Service(用于事务处理层),@Repository(用于数据库交互层) //一般写于类上方
属性注入注解:@Autowired(自动注入),@Qualifier(按名称 id 注入),@Resource(相当于自动注入与名称注入的结合体),@Value(为Spring对象的属性注入基本类型数据与String类型数据) //一般写于属性上方
其他注解:@Scope(用于配置修饰的类为单例还是多例,默认为单例),@Lazy(懒加载,用于修饰类,被修饰的类只有用到时才会被加载)
怎么使用?
在需要被Spring管理的类上写上注解,如
@Component //相当于在 application.xml 中配置一个 <bean id=“user” class=“com.xxx.xxx.user”>
public void user{
目录结构:
关键代码如下:
application.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"
default-lazy-init="true"
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">
<!-- 包扫描的位置,被扫描到的有注解的对象会被加入到Spring容器中,由Spring负责 实例化-->
<context:component-scan base-package="com.wanshi"/>
</beans>
实体类
user:
package com.wanshi.pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class user {
private String id;
private String username;
private String userpassword;
private String realname;
@Autowired
private role role;
static{
System.out.println("user对象创建了");
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpassword() {
return userpassword;
}
public void setUserpassword(String userpassword) {
this.userpassword = userpassword;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
}
role:
package com.wanshi.pojo;
import org.springframework.stereotype.Component;
@Component
public class role {
private int id;
private String role_name;
static {
System.out.println("role对象创建了");
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRole_name() {
return role_name;
}
public void setRole_name(String role_name) {
this.role_name = role_name;
}
}
userService:
package com.wanshi.service;
import com.wanshi.pojo.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class userService {
static{
System.out.println("userService对象创建了");
}
@Autowired
private user user;
}
Test(单元测试类):
package com.wanshi.test;
import com.wanshi.service.userService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class Test {
@org.junit.Test
public void Test() throws IOException {
// 初始化工厂对象,用于获取Spring容器中的bean对象
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
// 打印从Spring容器中拿到的userService对象
userService userservice= (userService) applicationContext.getBean("userService");
}
}
运行结果:
运行方式:
1.创建ApplicationContext对象时会加载application.xml配置文件,开启包扫描,将有注解的类交由Spring管理
2.获取userService的bean对象时,因它当中有@Autowired的user类,所以会自动注入到user属性中,当user类被Spring实例化时,因它当中又有@Autowired的role类,所以也会注入role的bean对象到user的role属性
3.只获取一个userService对象,就一次性获得了它所依赖的其他对象属性,而不用再一个一个new了
大家是不是感觉很方便呢?