注解开发,主要是将xml替换成注解。

<bean id="" class="" scope="" init-method="" destroy-method=""></bean>

<!--
id:使用@Component注解 括号中加id名
class:在类上使用@Component注解
init-method:在指定方法上使用@PostConstruct注解
destroy-method:在指定方法上使用@PreDestroy注解
-->

依赖注入使用@Autowired和@Qualifirer注解,而@Qualifier注解指定特定的实现类对象注解id

@Autowired
@Qualifier("accountDao")

Spring配置文件中 添加context约束,并设置扫描注释目录

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.company"></context:component-scan>

<!--
添加context约束名称
xmlns:context="http://www.springframework.org/schema/context"

在xis:schemaLocation中添加context约束路径
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd

设置自动扫描
<context:component-scan base-package="com.company"></context:component-scan>
-->

</beans>

添加依赖 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>com.company</groupId>
    <artifactId>Spring_annotation01</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <target>1.8</target>
                    <source>1.8</source>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>7.2.2.jre8</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.4</version>
        </dependency>
    </dependencies>
</project>

POJO目录下 Account.java

public class Account {
    private int id;
    private String name;
    private float money;

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public float getMoney() {
        return money;
    }

    public void setMoney(float money) {
        this.money = money;
    }
}

dao目录

AccountDao.java

public interface AccountDao {
    void saveAccount(Account account) throws SQLException;
}

AccountDaoImple.java

@Component("accountDao")
public class AccountDaoImpl implements AccountDao {
    @Override
    public void saveAccount(Account account) throws SQLException {
        System.out.println("saved");
    }
}

service目录

AccountService.java

public interface AccountService {
    void saveAccount(Account account);
}

AccountServiceImpl.java

@Component("accountService")
@Scope("prototype")
public class AccountServiceImpl implements AccountService {
    @Autowired
    @Qualifier("accountDao")
    private AccountDao accountDao;


    public AccountServiceImpl(){
        System.out.println("這是構造方法");
    }

    @PostConstruct
    public void init(){
        System.out.println("初始化之後執行");
    }
    @PreDestroy
    public void destory(){
        System.out.println("銷毀");
    }

    @Override
    public void saveAccount(Account account) {
        try {
            accountDao.saveAccount(account);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

测试

public class AnnotationTest {
    @Test
    public void test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
        AccountService accountService1 = applicationContext.getBean("accountService", AccountService.class);
        AccountDao accountDao = applicationContext.getBean("accountDao", AccountDao.class);
        System.out.println(accountService);
        System.out.println(accountService1);
        System.out.println(accountDao);
    }
}

 补充:

  1. 对于不同的模块,@Component又细分为@Controller、@Service、@Repository注解,分别对应Controller层,Service层,Dao层。
  2. 依赖注入,除了使用@Autowired和@Qualifirer注解,还可以使用@Resource注解,如:@Resource(name="accountDao")。但最好使用前者。
 posted on 2019-10-25 11:02  会飞的金鱼  阅读(107)  评论(0)    收藏  举报