spring使用注解开发

本文介绍了如何在Spring框架中使用AOP注解进行配置,包括@Component、@Repository、@Service和@Controller的区分,以及如何通过`@ComponentScan`和`@Bean`实现组件扫描和bean定义。重点讲解了如何从XML配置转向Java注解方式,并展示了如何在Java配置类中添加组件扫描和导入其他配置文件。
摘要由CSDN通过智能技术生成

我们使用注解前要先进行导入这个包 就是这里面的一个aop包。

在这里插入图片描述
写配置文件,我们使用这种注解头文件,开启注解,同时我们有添加了一个标签,组件扫描让其这个包下的所有组件都生效

<?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:component-scan base-package="aw"/>
    <!--    开启注解-->
    <context:annotation-config/>


</beans>

@Component组件用于pojo层
@Repository组件用于dao层
@Service组件用于service层
@Controller组件用于controle(servlet)层

这些注解的意思是都将他们注册到spring中。
我们以Component为例
@Component等价与 <bean id=“user” class=“aw.pojo.User”
@Value等价与<property name=“name” value=“阿威”/

package aw.pojo;

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

@Component
public class User {
    @Value("阿威")
    private String name;

    public void show(){
        System.out.println(name+"1111");
    }
}

同时我们还可以使用注解添加他的作用域。

package aw.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;

@Component
@Scope("singleton")
public class User {
    @Value("阿威")
    private String name;

    public void show(){
        System.out.println(name+"1111");
    }
}

xml是属于万能使用,复杂的使用。
注解只适用于简单的,当自己的类。

java开发

首先是我们创建一个类,这里我们的注解我们已经了解过了。

package aw.pojo.java;

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

@Component
@Scope("singleton")
public class User {
    @Value("阿威")
    private String name;

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}

现在我们不是写applicationContext.xml配置文件,而是写一个配置类,我们首先是写一个
@Configuration配置注解,他的作用相当于.xml,同时也将这个类注册到spring中。
@Bean注解就相当于<bean 标签,id是方法名,class类型是返回值类型。

package aw.pojo.java;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JavaConfig {

    @Bean
    public User getUser(){
        return new User();
    }

}

因为我们是通过java注解写所以要通过注解配置文件(AnnotationConfig)读取这个类,然后在通过id获取方法类型。

public class Test2 {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext javaConfig = new AnnotationConfigApplicationContext(JavaConfig.class);
        User getUser = (User) javaConfig.getBean("getUser");
        System.out.println(getUser);
    }
}

我们还可以在配置类中添加一些其他的注解
@ComponentScan(“aw.pojo.java”) 组件扫描 ( <context:component-scan base-package=“aw”/>)
@Import(JavaConfig2.class) 导入另一个配置文件( <import resource=""/)

@Configuration
@ComponentScan("aw.pojo.java")
@Import(JavaConfig2.class)
public class JavaConfig {

    @Bean
    public User getUser(){
        return new User();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值