如何在spring mvc中实现Spring引导@Autowired来实例化MongoRepository接口?

时间:2017-08-22 19:57:42

标签: spring mongodb annotations

这是一个严肃的问题" Springers" ...

我想使用MongoRepository。所以我去了spring.io website tutorial for MongoDB CRUD operations;

第一个问题是他们正在使用Spring启动。第二个问题是他们在接口上使用@Autowired。该注释似乎能够创建一个对象(以后称之为方法......)

所以....我使用普通的春季MVC并且@Autowired似乎没有在那里工作。

我如何&#34;实例化&#34; MongoRepository没有@Autowired mvc的package hello; import java.util.List; import org.springframework.data.mongodb.repository.MongoRepository; public interface CustomerRepository extends MongoRepository<Customer, String> { public Customer findByFirstName(String firstName); public List<Customer> findByLastName(String lastName); } 接口?

MongoRepository扩展接口

@Autowired

包含package hello; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application implements CommandLineRunner { @Autowired private CustomerRepository repository; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... args) throws Exception { repository.deleteAll(); // save a couple of customers repository.save(new Customer("Alice", "Smith")); repository.save(new Customer("Bob", "Smith")); // fetch all customers System.out.println("Customers found with findAll():"); System.out.println("-------------------------------"); for (Customer customer : repository.findAll()) { System.out.println(customer); } System.out.println(); // fetch an individual customer System.out.println("Customer found with findByFirstName('Alice'):"); System.out.println("--------------------------------"); System.out.println(repository.findByFirstName("Alice")); System.out.println("Customers found with findByLastName('Smith'):"); System.out.println("--------------------------------"); for (Customer customer : repository.findByLastName("Smith")) { System.out.println(customer); } } }

的课程
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Process uploaded CSV file for Seatac Noise</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>

<?php
error_reporting(E_ALL);
//echo "<pre>" . PHP_EOL;
$database = "jch";
$user     = "jch";
$password = "password";
$connector = 'jch.55.com';
$link = mysql_connect($connector,$user,$password);
mysql_select_db($database) or die( "Unable to select database");
$filter = $_POST["filter"];
$uploaddir = '/uploads';

$path = $uploaddir  . $filter . basename($_FILES['ufile']['name']);

if($ufile != none)
{ 
if (move_uploaded_file($_FILES['ufile']['tmp_name'], $path)) {
  echo "File is valid and was successfully uploaded.\n";

    $row = 0;
    if (($handle = fopen($path, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    $row++;

            if( strlen($data[4]) > 0 )          
            {

             $monitor_id = $data[1];                                                                                             
                 $time = date("Y-m-d H:i:s", strtotime($data[0]));  

             echo('<p>' . $time . ' rows processed: ' . $row . '</p>');

                 $noise_level = $data[2];                                                                                            
                 $flight_id = $data[4];                                                                                          
                 $aircraft_type = $data[3];                                                                                          

                 $query="INSERT INTO jch_noise_detail_copy (
                        monitor_id,                                                                                          
                        time,
                        noise_level,
                        flight_id,
                        aircraft_type) 
                        VALUES(" . 
                            "'" . $monitor_id . "','" .
                            $time . "'," .
                            $noise_level . ",'" .
                            $flight_id . "','" .
                            $aircraft_type . "')";

                        $result = mysql_query($query);
                        if( mysql_errno($link)!= 0 ){
                                echo $query . "\n";
                                echo mysql_errno($link) . ": " . mysql_error($link) . "\n"  ;
                        }
                }

        }  // end while
        mysql_close();


    }

}
else
    echo 'No Rows found matching this date or filter';  
}
mysql_close();
fclose($handle);
?>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

您应该使用#rateyo1 .jq-ry-normal-group svg { fill:purple; stroke:lime; stroke-width:20; } #rateyo1 .jq-ry-rated-group svg { fill:lime; stroke:purple; stroke-width:20; }<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.3.2/jquery.rateyo.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.3.2/jquery.rateyo.min.css" rel="stylesheet"/> <div> <div id="rateyo1" class="rateyo" data-rateyo-rating="1.5" data-rateyo-num-stars="5"> </div> </div>课程进行注释,并确保通过Spring的组件扫描将其选中。然后,您可以根据需要Repository将其分类。

要明确不要&#34;实例化&#34;界面。您声明了对该接口的实现的依赖(通过@Repository)并假设Spring Data MongDB在类路径上Spring将创建一个运行时实现并使其可用作要注入的bean。