public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
public class UserService { @Autowired private UserRepository userRepository; public void addUser(User user) { userRepository.add(user); } }
<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.example</groupId> <artifactId>my-project</artifactId> <version>1.0.0</version> </project>
@Component public class UserService { @Autowired private RedisTemplate<String, User> redisTemplate; public User getUser(String id) { String key = "user:" + id; User user = redisTemplate.opsForValue().get(key); if (user == null) { user = userRepository.getUser(id); redisTemplate.opsForValue().set(key, user); } return user; } }
Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); Producer<String, String> producer = new KafkaProducer<>(props); String topic = "my-topic"; String message = "Hello, Kafka!"; producer.send(new ProducerRecord<>(topic, message)); producer.close(); Consumer<String, String> consumer = new KafkaConsumer<>(props); consumer.subscribe(Collections.singletonList(topic)); ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(1)); for (ConsumerRecord<String, String> record : records) { System.out.println(record.value()); } consumer.close();
以上がJava 開発に不可欠なツールを明らかにします。どのツールに精通していますか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。