package com.example.boot3.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController
{
@Autowired
private StringRedisTemplate redisTemplate;
@GetMapping("/hello")
public String Hello(){
return "Hello";
}
@GetMapping("/incr")
public String incr(){
Long haha = redisTemplate.opsForValue().increment("haha");
return "增加后的为" + haha;
}
}
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>boot3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot3</name>
<description>boot3</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
server:
port: 9999
spring:
servlet:
multipart:
max-file-size: 10MB
data:
redis:
host: 192.168.92.129
port: 6379
database: 0
password: abc123
lettuce:
pool:
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 0
timeout: 5000
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<bean id="user" class="com.example.boot3.bean.User" scope="prototype">
<property name="id" value="1"></property>
<property name="name" value="Jack"></property>
</bean>
<bean id="cat" class="com.example.boot3.bean.Cat">
<property name="id" value="2"></property>
<property name="name" value="Rose"></property>
</bean>
</beans>
package com.example.boot3;
import com.example.boot3.bean.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan("com.example")//改变包扫描路径
@SpringBootApplication(scanBasePackages = "com.example.boot3")//手动指定扫描包位置
public class Boot3Application
{
public static void main(String[] args) {
var ioc = SpringApplication.run(Boot3Application.class, args);
// for (String name : ioc.getBeanDefinitionNames()) {
// System.out.println(name);
// }
// Object user = ioc.getBean("user");
// Object user1 = ioc.getBean("user");
// System.out.println(user == user1);
// System.out.println(ioc.getBean("fastsqlException"));
for (String s : ioc.getBeanNamesForType(Cat.class)) {
System.out.println(s);
}
for (String s : ioc.getBeanNamesForType(Dog.class)) {
System.out.println(s);
}
for (String s : ioc.getBeanNamesForType(User.class)) {
System.out.println(s);
}
Object pig = ioc.getBean("pig");
System.out.println(pig);
// for (String s : ioc.getBeanNamesForType(Sheep.class)) {
// System.out.println(s);
// }
Object sheep = ioc.getBean(Sheep.class);
System.out.println(sheep);
}
}
127.0.0.1:6381> shutdown
not connected> quit
[root@localhost ~]# redis-cli -a abc123 -p 6382 -c --raw
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6382> shutdown
not connected> quit
[root@localhost ~]# redis-server /myredis/redis6379.conf
[root@localhost ~]# redis-cli -a abc123 -p 6379 --raw
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> get haha
12
标签:框架,Spring,boot,System,springframework,Boot,println,org,ioc
From: https://blog.51cto.com/u_16322355/9569793