使用javacofig实现纯注解配置bean(最基本)

本文介绍了如何在Maven项目中引入Spring框架的webmvc和jdbc模块,并通过Java配置类替代XML配置。创建了一个名为Kuang的实现类,使用@Component注解将其注册到Spring容器中,并用@Bean注解配置了一个测试bean。在测试类中,通过AnnotationConfigApplicationContext加载配置并获取bean进行测试。
摘要由CSDN通过智能技术生成

首先在maven中肯定要有spring的jar包

<!--    Spring-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.7.RELEASE</version>
</dependency>

<!--    Spring-jdbc-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.7.RELEASE</version>
</dependency>

第一步创建一个实现类
Kuang.java

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

/**
 * 这是一个实现类
 */
@Component                    //这个注解的意思是说明这个类被spring接管了  注册到spring容器中了
public class Kuang {
    private String name;
    private int age;

    public String getName() {
        return name;
    }
    @Value("HUAHUA")
    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

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

第二 写一个java 充当xml文件
xml.java

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

/**
 * 这个相当于配置文件Appliction.xml
 */
@Configuration     //本质也是一个组件@Componet  代表这是一个配置类
//@ComponentScan("cn")    这个是指定要扫描的包  我这个实例都在一个包下面所以它没有也可以
public class xml {

    @Bean
    public Kuang get(){
        return new Kuang();//返回要注入到bean的对象
    }
}

测试类运行
test.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 这是一个测试类
 */

public class test {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(xml.class);//这个地方反射的是充当xml文件的java类反射
        Kuang bean = (Kuang)context.getBean("get");//这个get来自xml.java中配置了注解@Bean的那个方法
        System.out.println(bean.getName());
    }
}
除了使用 mybatis-config.xml 配置文件外,我们还可以使用 Java 注解配置 MyBatis,以下是一个示例: ``` @Configuration public class MyBatisConfig { @Bean public DataSource dataSource() { // 配置数据源 BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/test"); dataSource.setUsername("root"); dataSource.setPassword("123456"); return dataSource; } @Bean public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); // 配置 MyBatis 插件 Interceptor[] plugins = new Interceptor[] { new MyBatisInterceptor() }; sessionFactory.setPlugins(plugins); // 配置 MyBatis 映射文件 Resource[] mapperLocations = new PathMatchingResourcePatternResolver() .getResources("classpath*:com/example/mapper/*.xml"); sessionFactory.setMapperLocations(mapperLocations); return sessionFactory.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplate() throws Exception { return new SqlSessionTemplate(sqlSessionFactory()); } } ``` 在这个示例中,我们使用了 Spring 的注解配置 MyBatis。我们通过 dataSource() 方法配置了数据源,通过 sqlSessionFactory() 方法配置了 SqlSessionFactory,其中包括配置了插件和映射文件。最后,我们通过 sqlSessionTemplate() 方法配置了 SqlSessionTemplate。 需要注意的是,使用 Java 注解配置 MyBatis 时,需要在 pom.xml 文件中添加对 mybatis-spring 的依赖。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值