行业知识
Github 上优秀的 Java 项目推荐
以下是我觉得比较优秀的 Java 项目,包括一些开源的框架、工具和应用。每个项目都附有简要的说明和代码示例。
()- Spring Framework
Spring Framework 是一个流行的 Java 应用程序开发框架,它提供了各种各样的特性和功能,如控制反转、依赖注入、面向切面编程、模板及 JDBC 抽象层等。
一个简单的 Spring 应用程序示例:
()@Configuration@EnableSpringDataWebSupportpublic class AppConfig { @Bean public DataSource dataSource() { return new EmbeddedDatabaseBuilder() .setType(EmbeddedDatabaseType.HSQL) .addScript("classpath:schema.sql") .build(); }}@RestController@RequestMapping("/api")public class UserController { @Autowired private UserService userService; @RequestMapping(value = "/users", method = RequestMethod.GET) public List getUsers() { return userService.getUsers(); } @RequestMapping(value = "/users/{id}", method = RequestMethod.GET) public User getUser(@PathVariable("id") Long id) { return userService.getUser(id); }}@Service@Transactionalpublic class UserServiceImpl implements UserService { @Autowired private UserRepository userRepository; @Override public List getUsers() { return userRepository.findAll(); } @Override public User getUser(Long id) { return userRepository.findOne(id); }} - Apache Kafka
Apache Kafka 是一种高吞吐量的分布式消息队列系统,它可用于将消息从一个应用程序传输到另一个应用程序或系统。Kafka 设计用于处理大规模数据,并具有非常高的吞吐量和可扩展性。
一个简单的 Kafka 生产者代码示例:
Properties props = new Properties();props.put("bootstrap.servers", "localhost:9092");props.put("acks", "all");props.put("retries", 0);props.put("batch.size", 16384);props.put("linger.ms", 1);props.put("buffer.memory", 33554432);props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");Producer producer = new KafkaProducer(props);for (int i = 0; i 一个简单的 Kafka 消费者代码示例:
Properties props = new Properties();props.put("bootstrap.servers", "localhost:9092");props.put("group.id", "test");props.put("enable.auto.commit", "true");props.put("auto.commit.interval.ms", "1000");props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");KafkaConsumer consumer = new KafkaConsumer(props);consumer.subscribe(Arrays.asList("myTopic"));while (true) { ConsumerRecords records = consumer.poll(100); for (ConsumerRecord record : records) System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());} - Apache Storm
Apache Storm 是一种分布式实时计算系统,它可以处理大规模数据流,并且能够实时处理和计算数据。Storm 可以用于处理复杂的数据流分析工作,例如实时风险分析、实时广告分析以及实时推荐系统等。
一个简单的 Storm 拓扑代码示例:
TopologyBuilder builder = new TopologyBuilder();builder.setSpout("spout", new RandomSentenceSpout(), 5);builder.setBolt("split", new SplitSentenceBolt(), 8).shuffleGrouping("spout");builder.setBolt("count", new WordCountBolt(), 12).fieldsGrouping("split", new Fields("word"));Config conf = new Config();conf.setDebug(false);LocalCluster cluster = new LocalCluster();cluster.submitTopology("word-count", conf, builder.createTopology());Thread.sleep(10000);cluster.killTopology("word-count");cluster.shutdown(); - Apache Lucene
Apache Lucene 是一种全文搜索引擎库,它可以快速创建索引和搜索大量文本内容。Lucene 提供了各种各样的搜索和过滤功能,包括词条搜索、短语搜索、模糊搜索和范围搜索等。
一个简单的 Lucene 代码示例:
Directory directory = FSDirectory.open(Paths.get("index"));Analyzer analyzer = new StandardAnalyzer();IndexWriterConfig config = new IndexWriterConfig(analyzer);IndexWriter writer = new IndexWriter(directory, config);Document doc = new Document();doc.add(new TextField("title", "Lucene in Action", Store.YES));doc.add(new TextField("author", "Erik Hatcher", Store.YES));doc.add(new TextField("publisher", "Manning Publications", Store.YES));writer.addDocument(doc);writer.close();IndexReader reader = DirectoryReader.open(directory);IndexSearcher searcher = new IndexSearcher(reader);QueryParser parser = new QueryParser("title", analyzer);Query query = parser.parse("Lucene");TopDocs results = searcher.search(query, 10);ScoreDoc[] hits = results.scoreDocs;for (int i = 0; i - Apache Tomcat
Apache Tomcat 是一种流行的 Java Servlet 容器和 HTTP 服务器,用于运行 Java Web 应用程序。Tomcat 是一个轻量级的容器,它可以在多种操作系统上运行,并且可以与多种 Web 服务器集成。
一个简单的 Tomcat 应用程序示例:
@WebServlet(urlPatterns = "/hello")public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println("Hello, World!"); }}public class App { public static void main(String[] args) throws Exception { Tomcat tomcat = new Tomcat(); tomcat.setPort(8080); Context ctx = tomcat.addContext("", new File(".").getAbsolutePath()); Tomcat.addServlet(ctx, "hello", new HelloServlet()); ctx.addServletMappingDecoded("/hello", "hello"); tomcat.start(); tomcat.getServer().await(); }} - Apache Maven
Apache Maven 是一种流行的构建工具,用于自动化构建、测试和部署 Java 应用程序。Maven 使用一种基于 XML 的配置文件格式来定义构建过程,并提供了各种各样的插件和依赖管理工具。
一个简单的 Maven POM 文件示例:
4.0.0 com.example my-app 1.0-SNAPSHOT junit junit 4.11 test
- Apache Struts
Apache Struts 是一种流行的 Java Web 应用程序开发框架,它是基于 MVC(模型 - 视图 - 控制器)设计模式构建的。Struts 提供了各种各样的特性和功能,如表单验证、数据绑定、国际化支持、多种视图技术以及安全管理等。
一个简单的 Struts2 应用程序示例:
/hello.jsp public class HelloAction extends ActionSupport { public String execute() throws Exception { return SUCCESS; }}Hello, World!
8.以下是我觉得比较优秀的 Java 项目,包括一些开源的框架、工具和应用。每个项目都附有简要的说明和代码示例。
- Spring Framework
Spring Framework 是一个流行的 Java 应用程序开发框架,它提供了各种各样的特性和功能,如控制反转、依赖注入、面向切面编程、模板及 JDBC 抽象层等。
一个简单的 Spring 应用程序示例:
@Configuration@EnableSpringDataWebSupportpublic class AppConfig { @Bean public DataSource dataSource() { return new EmbeddedDatabaseBuilder() .setType(EmbeddedDatabaseType.HSQL) .addScript("classpath:schema.sql") .build(); }}@RestController@RequestMapping("/api")public class UserController { @Autowired private UserService userService; @RequestMapping(value = "/users", method = RequestMethod.GET) public List getUsers() { return userService.getUsers(); } @RequestMapping(value = "/users/{id}", method = RequestMethod.GET) public User getUser(@PathVariable("id") Long id) { return userService.getUser(id); }}@Service@Transactionalpublic class UserServiceImpl implements UserService { @Autowired private UserRepository userRepository; @Override public List getUsers() { return userRepository.findAll(); } @Override public User getUser(Long id) { return userRepository.findOne(id); }} - Apache Kafka
Apache Kafka 是一种高吞吐量的分布式消息队列系统,它可用于将消息从一个应用程序传输到另一个应用程序或系统。Kafka 设计用于处理大规模数据,并具有非常高的吞吐量和可扩展性。
一个简单的 Kafka 生产者代码示例:
Properties props = new Properties();props.put("bootstrap.servers", "localhost:9092");props.put("acks", "all");props.put("retries", 0);props.put("batch.size", 16384);props.put("linger.ms", 1);props.put("buffer.memory", 33554432);props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");Producer producer = new KafkaProducer(props);for (int i = 0; i 一个简单的 Kafka 消费者代码示例:
Properties props = new Properties();props.put("bootstrap.servers", "localhost:9092");props.put("group.id", "test");props.put("enable.auto.commit", "true");props.put("auto.commit.interval.ms", "1000");props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");KafkaConsumer consumer = new KafkaConsumer(props);consumer.subscribe(Arrays.asList("myTopic"));while (true) { ConsumerRecords records = consumer.poll(100); for (ConsumerRecord record : records) System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());} - Apache Storm
Apache Storm 是一种分布式实时计算系统,它可以处理大规模数据流,并且能够实时处理和计算数据。Storm 可以用于处理复杂的数据流分析工作,例如实时风险分析、实时广告分析以及实时推荐系统等。
一个简单的 Storm 拓扑代码示例:
TopologyBuilder builder = new TopologyBuilder();builder.setSpout("spout", new RandomSentenceSpout(), 5);builder.setBolt("split", new SplitSentenceBolt(), 8).shuffleGrouping("spout");builder.setBolt("count", new WordCountBolt(), 12).fieldsGrouping("split", new Fields("word"));Config conf = new Config();conf.setDebug(false);LocalCluster cluster = new LocalCluster();cluster.submitTopology("word-count", conf, builder.createTopology());Thread.sleep(10000);cluster.killTopology("word-count");cluster.shutdown(); - Apache Lucene
Apache Lucene 是一种全文搜索引擎库,它可以快速创建索引和搜索大量文本内容。Lucene 提供了各种各样的搜索和过滤功能,包括词条搜索、短语搜索、模糊搜索和范围搜索等。
一个简单的 Lucene 代码示例:
Directory directory = FSDirectory.open(Paths.get("index"));Analyzer analyzer = new StandardAnalyzer();IndexWriterConfig config = new IndexWriterConfig(analyzer);IndexWriter writer = new IndexWriter(directory, config);Document doc = new Document();doc.add(new TextField("title", "Lucene in Action", Store.YES));doc.add(new TextField("author", "Erik Hatcher", Store.YES));doc.add(new TextField("publisher", "Manning Publications", Store.YES));writer.addDocument(doc);writer.close();IndexReader reader = DirectoryReader.open(directory);IndexSearcher searcher = new IndexSearcher(reader);QueryParser parser = new QueryParser("title", analyzer);Query query = parser.parse("Lucene");TopDocs results = searcher.search(query, 10);ScoreDoc[] hits = results.scoreDocs;for (int i = 0; i - Apache Tomcat
Apache Tomcat 是一种流行的 Java Servlet 容器和 HTTP 服务器,用于运行 Java Web 应用程序。Tomcat 是一个轻量级的容器,它可以在多种操作系统上运行,并且可以与多种 Web 服务器集成。
一个简单的 Tomcat 应用程序示例:
@WebServlet(urlPatterns = "/hello")public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println("Hello, World!"); }}public class App { public static void main(String[] args) throws Exception { Tomcat tomcat = new Tomcat(); tomcat.setPort(8080); Context ctx = tomcat.addContext("", new File(".").getAbsolutePath()); Tomcat.addServlet(ctx, "hello", new HelloServlet()); ctx.addServletMappingDecoded("/hello", "hello"); tomcat.start(); tomcat.getServer().await(); }} - Apache Maven
Apache Maven 是一种流行的构建工具,用于自动化构建、测试和部署 Java 应用程序。Maven 使用一种基于 XML 的配置文件格式来定义构建过程,并提供了各种各样的插件和依赖管理工具。
一个简单的 Maven POM 文件示例:
4.0.0 com.example my-app 1.0-SNAPSHOT junit junit 4.11 test
- Apache Struts
Apache Struts 是一种流行的 Java Web 应用程序开发框架,它是基于 MVC(模型 - 视图 - 控制器)设计模式构建的。Struts 提供了各种各样的特性和功能,如表单验证、数据绑定、国际化支持、多种视图技术以及安全管理等。
一个简单的 Struts2 应用程序示例:
/hello.jsp public class HelloAction extends ActionSupport { public String execute() throws Exception { return SUCCESS; }}Hello, World!
上一篇:sql怎么连不上服务器?