亲宝软件园·资讯

展开

Java之Spring注解开发 Java之Spring注解开发案例详解

雪潇潇 人气:0
想了解Java之Spring注解开发案例详解的相关内容吗,雪潇潇在本文为您仔细讲解Java之Spring注解开发的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java之Spring注解,Java之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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://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.example.springannotation" />
    <context:annotation-config></context:annotation-config>
    <!-- <bean id="cat" class="com.example.springannotation.dao.Cat"/>
    <bean id="people" class="com.example.springannotation.dao.People"/>-->
</beans>

注解的支持:

//@Component 等价于<bean id="pepople" class="com.example.springannotation.dao.People" />
@Component
public class People {
    @Autowired(required = false)
    @Value("1235") //相当<property name="id " value="1235"/>
    private int id;
    @Autowired(required = false)
    private String name ="ming";
    
    @Value("qing") //相当<property name="name " value="qing"/>
    public void setName(@Nullable String name) {
        this.name = name;
    }
}

衍生的注解
@Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层!

这四个注解功能都是一样的,都是代表将某个类注册到Spring中,装配Bean。

@Scope("singleton") //singleton:标识单例模式,prototype:标识原型模式 、request:标识请求模式、session:标识会话模式

xml 与注解:

xml与注解最佳实践:

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

JAVA的方式配置Spring

// 配置类 代替 beans.xml
import com.example.springannotation.dao.Cat;
import com.example.springannotation.dao.People;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@ComponentScan("com.example.springannotation")
@Import(WwConfig.class)  //引入第二个配置
public class AppConfig {
    //注朋一个bean 相当于当于我们之前写的一个bean标签
    //这个方法的名字,就相当于bean标签中的id属性
    //这个方法的返回价,就和当了bean标签中的class属性
    @Bean
    public People getPeople(){
        return new People();
    }

    @Bean
    public Cat getCat(){
        return new Cat();
    }
}

import org.springframework.context.annotation.Configuration;
@Configuration
public class WwConfig {
}
//测试类
import com.example.springannotation.config.AppConfig;
import com.example.springannotation.dao.People;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@SpringBootTest
class SpringannotationApplicationTests {
    @Test
    void contextLoads() {
        如果完全使用了配置类方式做,
        // 我们就只能通过 AnnotationConfig 上下文来获取容器,通过配置类的class对象加载!
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        People people = (People) context.getBean("getPeople");
        System.out.println(people.toString());
    }
}

加载全部内容

相关教程
猜你喜欢
用户评论